Difference between Map() and Filter() in Python.

Ankur Ghogale
2 min readAug 23, 2018

--

Many a times working on python programming can inevitably bring you across situations where you have to manipulate data and you have a whole set of functions with very little difference in functionality. To avoid these situations you use loops, control statements, and many other substitutes, which can sometimes show you as a beginner.

Map and Filter are among the confusing functions but important ones as well. Lets take an example to understand their difference.

Map()

The basic function of map() is to manipulate iterables. Map executes all the conditions of a function on the items in the iterable. In the above example, we have multiplied each element in the range 0–10 with 2 which gives us completely new list of elements. Map function takes all elements and allows you to apply a function on it and then passes it to the output which can have same as well as different values .

Now, applying same action using both the functions we see how their functionality differs. Here map has checked the condition applied on the element one by one and passed a boolean output.

Filter()

As the name suggests, it is used to filter the iterables as per the conditions. Filter filters the original iterable and passes the items that returns True for the function provided to filter. Therefore only the items in the iterables can be expected to be seen in the output. In the above example the condition is given in the form of lambda function and the elements which satisfy the condition are given in the list. The elements which are divisible by 2 are left and others are filtered out.

Now, applying same condition we see filter has not multiplied the elements with 2 as done by map, whereas it has passed the same list of elements.

Map takes all objects in a list and allows you to apply a function to it whereas Filter takes all objects in a list and runs that through a function to create a new list with all objects that return True in that function. Hence, knowing the functionality and using it accordingly is important as both the functions have very minute difference in there functionality.

--

--

Ankur Ghogale

Data Analyst | Data Science Enthusiast | Traveller Check my Instagram profile ankur_ghogale https://instagram.com/ankur_ghogale?igshid=otnc9f7mjcpi