Python Tutorial

Python Variable

Python Operators

Python Sequence

Python String

Python Flow Control

Python Functions

Python Class and Object

Python Class Members (properties and methods)

Python Exception Handling

Python Modules

Python File Operations (I/O)

Python functional programming - map(), filter() and reduce()

Functional programming is a programming paradigm that emphasizes the use of functions to solve problems, rather than relying on mutable data and imperative statements. In Python, three built-in functions - map(), filter(), and reduce() - support functional programming techniques.

  • map() function: The map() function applies a function to each element of an iterable and returns a new iterable with the results. Here's an example:
numbers = [1, 2, 3, 4, 5]
squares = map(lambda x: x**2, numbers)
print(list(squares)) # Output: [1, 4, 9, 16, 25]

In this example, we use map() to apply the lambda function x**2 to each element of the list numbers. The result is a new iterable squares that contains the squares of the numbers in the original list.

  • filter() function: The filter() function applies a function to each element of an iterable and returns a new iterable with the elements for which the function returns True. Here's an example:
numbers = [1, 2, 3, 4, 5]
even_numbers = filter(lambda x: x % 2 == 0, numbers)
print(list(even_numbers)) # Output: [2, 4]

In this example, we use filter() to apply the lambda function x % 2 == 0 to each element of the list numbers. The result is a new iterable even_numbers that contains only the even numbers from the original list.

  • reduce() function: The reduce() function applies a function to the elements of an iterable in a cumulative way, from left to right, to reduce the iterable to a single value. Here's an example:
from functools import reduce
numbers = [1, 2, 3, 4, 5]
product = reduce(lambda x, y: x*y, numbers)
print(product) # Output: 120

In this example, we use reduce() to apply the lambda function x*y to the elements of the list numbers in a cumulative way, from left to right. The result is the product of all the numbers in the list.

In summary, map(), filter(), and reduce() are three built-in functions in Python that support functional programming techniques. They allow you to apply functions to iterables in various ways to transform, filter, and reduce data.

  1. How to use map() function in Python:

    • Description: Apply a function to all elements of an iterable using map().
    • Code:
      numbers = [1, 2, 3, 4, 5]
      squared = map(lambda x: x**2, numbers)
      
  2. Filtering elements with filter() in Python:

    • Description: Select elements from an iterable based on a specified condition using filter().
    • Code:
      numbers = [1, 2, 3, 4, 5]
      even_numbers = filter(lambda x: x % 2 == 0, numbers)
      
  3. Aggregating values with reduce() in Python:

    • Description: Combine elements of an iterable into a single result using reduce().
    • Code:
      from functools import reduce
      numbers = [1, 2, 3, 4, 5]
      sum_result = reduce(lambda x, y: x + y, numbers)
      
  4. Applying functions to iterables with map() in Python:

    • Description: Demonstrate how map() simplifies applying a function to each element of an iterable.
    • Code:
      words = ["apple", "banana", "cherry"]
      uppercased = map(str.upper, words)
      
  5. Conditionally selecting elements with filter() in Python:

    • Description: Use filter() to selectively include elements based on a specified condition.
    • Code:
      temperatures = [25, 30, 15, 40, 10]
      high_temps = filter(lambda x: x > 30, temperatures)
      
  6. Reducing sequences with reduce() in Python:

    • Description: Utilize reduce() to successively apply a function to pairs of elements, reducing the sequence to a single result.
    • Code:
      product = reduce(lambda x, y: x * y, [1, 2, 3, 4])
      
  7. Lambda expressions and functional programming in Python:

    • Description: Employ lambda expressions for concise anonymous functions in functional programming.
    • Code:
      square = lambda x: x**2
      result = square(5)