+ 8

What is difference between 'filter' and 'map' in Python?

I am beginner and started to learn Python since couple of months. I didn't get much clear idea about filter and map in Python. Even I noticed in challenges it looks same. I don't know of there is no difference why Python has two different things. I know there may be something different. Thank You.

27th May 2019, 12:42 PM
Felina
Felina - avatar
4 Answers
+ 9
map() applies a function to every item of an iterable. filter() returns all elements of an iterable for which a function is true. # Multiplies each element by 2 print(list(map(lambda x: x*2, [1,2,3,4]))) # [2, 4, 6, 8] # Returns all elements greater than 2 print(list(filter(lambda x: x>2, [1, 2, 3, 4]))) # [3, 4]
27th May 2019, 1:24 PM
Diego
Diego - avatar
+ 14
U can understand better in thies video https://youtu.be/kj850Y8y8FI
29th May 2019, 10:46 AM
Vijay(v-star🌟)
Vijay(v-star🌟) - avatar
+ 4
Thank You Diego Bhavya Bhav and Jan Markus for answers!
28th May 2019, 7:40 AM
Felina
Felina - avatar
+ 1
In map you can use multiple iterables. In filter only one iterable can be used.
5th Jun 2020, 10:40 AM
pooja panchal
pooja panchal - avatar