+ 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.
4 Respostas
+ 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]
+ 14
U can understand better in thies video
https://youtu.be/kj850Y8y8FI
+ 4
+ 1
In map you can use multiple iterables.
In filter only one iterable can be used.