+ 1
Filter and map difference?
What's the main difference between both filter and map funcitons? The mult function just multiply a number * 2, shouldn't both codes giving back the same list as result? lis = [1,2,3,4,5,6,] lisu = list(map(mult, lis)) [5, 10, 15, 20, 25, 30] lis = [1,2,3,4,5,6,] lisu = list(filter(mult, lis)) [1,2,3,4,5,6,]
4 Réponses
+ 1
No, it should not.
map and filter is not the same. That's why they have differend names.
If you map a func to a list, the func will applied to all members of the list. By the way, your mult func multiplys by 5!
The filter takes values out from your list, if the result of the func is "False". Your mult func is True for all values in your list. So you get a copy of your list as result.
Maybe, if you put a " 0" in the list, the result of mult func 0*5 = 0. This could be give a False and the 0 has to be filtered out in the result list.
+ 3
for any iterable a,
filter retains values in a that fulfill a condition
map applies a function to all values
+ 1
Thank you so much for the explanation that clarified it a lot and yes i mistook, it does is multiplying by 5, I tried it as you say and yes filter takes out the zero. I get the difference now, thank you again man! Greetings
0
You are wecome