Python Map, Filter and Reduce

In this tutorial, you’ll learn about three of Python’s most powerful functions: map(), filter(), and reduce().

Functional programming paradigms include Map, Filter, and Reduce. They allow the programmer (you) to write simpler, shorter code by removing the need for complexity such as loops and branching.

These are three functions that help with a functional programming approach. We’ll go over each one one by one to see how they’re used.

Map

The map() function returns a map object (an iterator) actually contains the results of applying the given function to each item of an iterable (list, tuple etc.)

The map() function in python has the following syntax:

map(function, iterables)

Example :

def square(a):
    return a*a

res = map(square,[1,2,3,4])
print(res) # this will print map object
print(list(res)) # this will print result list
Output : 
<map object at 0x000001C1A423ECE0>
[1, 4, 9, 16]

In above example each element of list pass as parameter in square() and return their square.

We can also use lambda expression with map to achieve above result.

Example :

res = map(lambda x : x*x , [1,2,3,4])
print(list(res))
Output : [1, 4, 9, 16]

In above example lambda x : x*x lines worked as a function, it returns a square of each element of the list.

Filter

The filter() method filters a series using a function that checks if each element in the sequence is true or not.

Syntax :

filter(function, sequence)

Example : Find Even Numbers

def find(num):
    if num % 2 == 0:
        return num
    return False


seq = [1,2,3,4,5,6]
res = filter(find,seq)
print(list(res))
Output : [2, 4, 6]

In above example each element of list pass as parameter in find() and check if it is even number then return those number.

We can also use lambda expression with map to achieve above result.

seq = [1,2,3,4,5,6]
res = filter(lambda x : x % 2 ==0,seq)
print(list(res))
Output : [2, 4, 6]

In above example lambda x : x % 2 ==0 line return element if condition is true.

Reduce

Reduce() is not the same as map() or filter() . It doesn’t generate a new list depending on the function and iterable we provided. Instead, a single value is returned.

Reduce() is no longer a built-in function in Python 3, and it must be found in the functools package.

Working :  

  • The first two elements of the sequence are chosen in the first step, and the result is produced.
  • The result is then saved again after applying the same function to the previously obtained result and the number just preceding the second element.
  • This process continues till no more elements are left in the container.
  • The final returned result is returned and printed on console.

Example : Sum of list element

import functools as fn

def sum(x,b):
    print("-----------")
    print(x)
    print(b)
    print("sum-->",x + b)
    return x + b

li = [2,8,1,9]
res = fn.reduce(sum,li)
print("result is -->",res)
-----------
2
8
sum--> 10
-----------
10
1
sum--> 11
-----------
11
9
sum--> 20
result is --> 20

We can also use lambda expression with map to achieve above result.

li = [2,8,1,9]
res = fn.reduce(lambda a,b : a + b,li)
print("result is -->",res)
result is --> 20

Please refer Python Lambda functions for more details.

That is it for today, hope it helps.
If you have any suggestion for this article please make a comment in comment section below.

If you like this article, you can buy me a coffee. Thanks!

Python Map, Filter and Reduce

Leave a Reply

Your email address will not be published. Required fields are marked *

Scroll to top