Day 23: Functional Programming Concepts (map, filter, reduce) in Python

Day 23: Functional Programming Concepts (map, filter, reduce) in Python

Unlocking the Power of Functional Programming: Exploring map, filter, and reduce in Python

Today, on the twenty-third day of our Python learning journey, we delve into essential concepts: map, filter, and reduce functions. Join us as we explore the significance and widespread use of these powerful tools.

Map

In Python, the map() function is used to apply a specified function to each item in an iterable (such as a list, tuple, or set) and returns a map object (an iterator). The function is then applied to each item in the iterable, and the result is yielded by the iterator.

syntax

map(function, iterable)
  • function: The function to apply to each item in the iterable.

  • iterable: The iterable (e.g., list, tuple, set) whose elements will be passed as arguments to the function.

# Define a function
def square(x):
    return x ** 2

# Define an iterable
numbers = [1, 2, 3, 4, 5]

# Apply the function to each item in the iterable using map
squared_numbers = map(square, numbers)

# Convert the map object to a list
squared_numbers_list = list(squared_numbers)

print(squared_numbers_list)  # Output: [1, 4, 9, 16, 25]

Reduce

In Python, the reduce() function is used to apply a function cumulatively to the items of an iterable, from left to right, so as to reduce the iterable to a single value. It is part of the functools module in Python 3.x and needs to be imported before use.

from functools import reduce

# Define a function to add two numbers
def add(x, y):
    return x + y

# Define an iterable
numbers = [1, 2, 3, 4, 5]

# Use reduce() to find the sum of the numbers
sum_of_numbers = reduce(add, numbers)

print(sum_of_numbers)  # Output: 15

Filter

In Python, the filter() function is used to construct a new iterable from the elements of an existing iterable (such as a list, tuple, or set) for which a specified function returns true. It filters out the elements based on the function's evaluation.

# Define a function to check if a number is even
def is_even(num):
    return num % 2 == 0

# Define an iterable
numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

# Use filter() to get the even numbers from the list
even_numbers = filter(is_even, numbers)

# Convert the filter object to a list
even_numbers_list = list(even_numbers)

print(even_numbers_list)  # Output: [2, 4, 6, 8, 10]