Functional programming in Python is supported by three powerful built-in functions — map(), reduce(), and filter(). These functions enable efficient data transformation and processing by applying operations to entire iterables (like lists or tuples) without using explicit loops.
Below are examples of Map Reduce and Filter Operations in Python:
Map Function in Python
map() function returns a map object(which is an iterator) of the results after applying the given function to each item of a given iterable (list, tuple, etc.).
Syntax:
map(fun, iter)
Parameters:
- fun: It is a function to which map passes each element of given iterable.
- iter: iterable object to be mapped.
Example: The following code doubles each element in a list using map().
def double(n):
return n * 2
n = [5, 6, 7, 8]
res = map(double, n)
print(list(res))
Output
[10, 12, 14, 16]
Reduce Function in Python
The reduce function is used to apply a particular function passed in its argument to all of the list elements mentioned in the sequence passed along.This function is defined in "functools" module.
Syntax:
reduce(func, iterable[, initial])
Parameters:
- fun: It is a function to execute on each element of the iterable object.
- iter: It is iterable to be reduced
- initial (optional): Initial value for the accumulator.
Example: The following code calculates the product of all elements in a list using reduce().
import functools
n = [1, 2, 3, 4]
prod = functools.reduce(lambda x, y: x * y, n)
print(prod)
Output
Product of list elements: 24
Filter Function in Python
The filter() method filters the given sequence with the help of a function that tests each element in the sequence to be true or not.
Syntax:
filter(function, sequence)
Parameters:
- function: function that tests if each element of a sequence is true or not.
- sequence: sequence which needs to be filtered, it can be sets, lists, tuples, or containers of any iterators.
Example: The following code filters out even numbers from a list using filter().
def is_even(n):
return n % 2 == 0
n = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
en = filter(is_even, n)
print(list(en))
Output
[2, 4, 6, 8, 10]
Combined Examples
Example 1: Sum of squares of even numbers
from functools import reduce
n = [1, 2, 3, 4, 5, 6]
e = filter(lambda x: x % 2 == 0, n)
s = map(lambda x: x**2, e)
res = reduce(lambda x, y: x + y, s)
print(res)
Output
56
Explanation:
- filter(lambda x: x % 2 == 0, n): extracts even numbers [2, 4, 6].
- map(lambda x: x**2, e): squares each even number and returns [4, 16, 36].
- reduce(lambda x, y: x + y, s): sums all squared values → 4 + 16 + 36 = 56.
Example 2: Product of positive numbers
from functools import reduce
n = [-3, -1, 2, 4, -2, 5]
res = reduce(lambda x, y: x * y,map(lambda x: x,filter(lambda x: x > 0, n)))
print(res)
Output
40
Explanation:
- filter(lambda x: x > 0, n): selects positive numbers [2, 4, 5].
- map(lambda x: x, …): keeps them unchanged.
- reduce(lambda x, y: x * y, …): multiplies all → 2 × 4 × 5 = 40.
Example 3: Sum of lengths of words with vowels
from functools import reduce
w = ["sky", "apple", "tree", "gym", "orange"]
res = reduce(lambda a, b: a + b,map(lambda w: len(w),filter(lambda w: any(v in w for v in 'aeiou'), w)))
print(res)
Output
15
Explanation:
- filter(lambda w: any(v in w for v in 'aeiou'), w): keeps words containing vowels → ['apple', 'tree', 'orange'].
- map(lambda w: len(w), …): converts each to its length [5, 4, 6].
- reduce(lambda a, b: a + b, …): adds all lengths → 5 + 4 + 6 = 15.