Magical functions in Python — Map, Reduce and Filter (Loop killers)

Kunal Gandhi
2 min readJun 17, 2021

--

Python provides amazing functions which makes developer’s work more easy and efficient.

Let’s talk about few amazing functions.

Map()

What is the Super Power of Map function?
Basically Map function applies another function let’s say function to do square on the each item of iterable (List in most of the cases😁).

Map function take 2 Arguments. 1st is function and 2nd is Iterables for ex. List.

Syntax :

map(function , Iterable[,..])

Example :

We have one function square_list which returns square of input variable x.

def square_list(x):
return x*x

li=range(10) # li=[0,1,2,3,4,5,6,7,8,9]
print(list(map(square_list,li)))Output : [0, 1, 4, 9, 16, 25, 36, 49, 64, 81]

if you run above code, It will do square of each element in the list and returns list. If you do not use map function the you have to write following code:

def square_list(x):
return x*x
li=range(10) # li=[0,1,2,3,4,5,6,7,8,9]for i in li:
print(square_list(i))

Map is compact plus efficient as well. It only takes 0.00099 sec to execute above code for the list of 1000 number on the other hand for loop takes 0.0608 sec to execute same. Which is 615 times more. It may doesn’t impact on small code but when you are doing big time consuming operations then map is real savior . 😎

Reference : https://python-reference.readthedocs.io/en/latest/docs/functions/map.html

Reduce()

Now, What Reduce function do?

Reduce function is also take 2 arguments 1 function and list.

Syntax :

reduce(function , Iterable[,..])

But as it’s name it reduces the size of list. It only returns one value. So what it does is it takes 2 element from the list and perform the operation of another function and returns one value as output. It continuously perform this operation until there is only one element left.

For example, In below code snippet there is function which returns sum of 2 variable. With reduce you can use this function to do the sum of whole list without using for loop.

from functools import reducedef sum_list(x,y):
return x+y
li=range(10) # li=[0,1,2,3,4,5,6,7,8,9]print(reduce(sum_list,li))#Output : 45

Reduce is also efficient compare to loops.

Note : reduce is part of functools so you can’t use it directly without importing first.

Reference : https://python-reference.readthedocs.io/en/latest/docs/functions/reduce.html

Filter()

Now what filter can do?

As it name suggest it’s filter out elements. Let’s say you have list of element and you want to filter out all the element which are greater then 0 and less then 5. In that case filter function will do the job.

Syntax :

filter(function,iterable)

For example, here filter_list is function which return only if value of variable is greater than 0 and less than 5. filter use this function on each element of list and returns list of element which satisfy that condition.

def filter_list(x):
if x>0 and x<5:
return x
li=range(10) # li=[0,1,2,3,4,5,6,7,8,9]print(list(filter(filter_list,li)))#Output : [1,2,3,4]

Reference : https://python-reference.readthedocs.io/en/latest/docs/functions/filter.html

Conclusion : These python in built functions are amazing. It improves a performance and also improves the readability and make code look simple. These are widely used in the scenarios where we need to perform large computations.

--

--

Kunal Gandhi

Senior Analyst -Data Science. IT Engineer. Interested in AI. Love coding.(Python,Js,Java)