Map, Filter, Reduce

map(), filter(), and reduce() are built-in Python functions that are commonly used for functional programming.

map() function applies a given function to all items of an iterable (such as a list or tuple) and returns a new iterable with the modified items. The function that is used with map() is called for each item of the input iterable and its return value is used to create the new iterable. The basic syntax for using map() is as follows:

map(function, iterable)

For example, to square all the elements of a list of numbers, you can use the map() function with a lambda function as the first argument:

numbers = [1, 2, 3, 4, 5]
squared_numbers = list(map(lambda x: x**2, numbers))
print(squared_numbers)  # Output: [1, 4, 9, 16, 25]

filter() function is used to filter the items of an iterable based on a given function and return a new iterable with only the items that satisfy the function’s condition. The function that is used with filter() is called for each item of the input iterable and its return value is used to determine whether or not the item will be included in the new iterable. The basic syntax for using filter() is as follows:

filter(function, iterable)

For example, to filter out all the even numbers from a list of numbers, you can use the filter() function with a lambda function as the first argument:

numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
odd_numbers = list(filter(lambda x: x % 2 != 0, numbers))
print(odd_numbers)  # Output: [1, 3, 5, 7, 9]

reduce() function is used to apply a given function to all items of an iterable and reduce the iterable to a single value. The function that is used with reduce() is called for each item of the input iterable and its return value is used to update the final result. The basic syntax for using reduce() is as follows:

from functools import reduce
reduce(function, iterable)
from functools import reduce
reduce(function, iterable)

For example, to find the product of all the elements of a list of numbers, you can use the reduce() function with a lambda function as the first argument:

from functools import reduce
numbers = [1, 2, 3, 4, 5]
product = reduce(lambda x, y: x*y, numbers)
print(product)  # Output: 120

map(), filter(), and reduce() are particularly useful when working with large amounts of data and when you want to perform a specific operation on each item of an iterable in a concise and efficient manner. These functions are widely used in functional programming and can be combined with other functional constructs such as lambda functions and list comprehensions to create more powerful and expressive code.

Map vs Filter vs Reduce

map()filter()reduce()
Applies a given function to all items of an iterable and returns a new iterable with the modified itemsFilters the items of an iterable based on a given function and returns a new iterable with only the items that satisfy the function’s conditionApplies a given function to all items of an iterable and reduces the iterable to a single value
Can be used with any type of iterableCan be used with any type of iterableShould be used with a list or any other iterable that has at least one element
Returns an iterator, so you need to convert it into a list or tuple or any other iterable to see the resultReturns an iterator, so you need to convert it into a list or tuple or any other iterable to see the resultReturns a single value
The function that is used with map() is called for each item of the input iterable and its return value is used to create the new iterableThe function that is used with filter() is called for each item of the input iterable and its return value is used to determine whether or not the item will be included in the new iterableThe function that is used with reduce() is called for each item of the input iterable and its return value is used to update the final result

These three functions are widely used in functional programming, these functions can be combined with other functional constructs such as lambda functions and list comprehensions to create more powerful and expressive code.