Understanding map, filter, and zip in Python
The purpose of this article is to understand how Python built-in functions map(), filter(),...
For further actions, you may consider blocking this person and/or reporting abuse
I almost never use
map
orfilter
but it's good to know how they work. I use list comprehensions all the time though.A tip: you don't need to call
list()
on an iterable, unless you have a good reason to (like to print it as an example) you can just... iterate it :)This is a good intro on "functional Python": Functional Programming HOWTO
I predominantly use comprehensions as well, I actually completely forgot you could just iterate the objects so good call! I'm going to update this.
Hi. Sorry I am not very experienced but I spotted a couple of problems in your final bit about
zip()
.First off you try to iterate over
even_numbers
without callinglist()
. I can see others have argued here thatlist()
is not necessary, and I am but a hobby programmer, so I know very little of the deeper layers of programming and maybe I am doing it wrong, but when I try to run the code snippets here, it only works if I calllist()
on thefilter
andmap
returns. Otherwise I get errors in thefor
loop (does not iterate in the loop withoutlist()
onfilter
, gives aTypeError: 'map' object is not subscriptable
.You set
even_numbers_index = 0
and then in thefor
loop you setsquared = even_numbers_squared[even_numbers_index]
but you never updateeven_numbers_index
so for each iteration in the loop you keep calling the same index number. When we extend the input list a bit tonumbers = [1,2,3,4,5,6,7,8]
the result then becomes[(2, 4), (4, 4), (6, 4), (8, 4)]
giving an error in output compared to the intended.It works fine if we use
zip()
though and skip the for loop:[(2, 4), (4, 16), (6, 36), (8, 64)]
This is just in case others like me come by this otherwise great post to learn about
map
,filter
andzip
(and I really mean that. Short and well explained - thanks for that)Full code I was running:
Good catches! Sorry to be late to responding, but I'll make sure to edit these, thank you.
But remember, in Python comprehensions should be the first try for what could be done in map and filter.
Compressions can typically replace all 3 (map, filter, reduce) and can be used with zip.
The concepts here are still important to know. Libraries like pandas utilize the map syntax, or using a different language like JavaScript.
Absolutely, and thanks for commenting this, I hope everyone sees this as well!
I suppose you have to use the list() function on the result these three functions return because that's just an iterable instead of a list, right?
Correct. Map, filter, and zip objects are iterables, but we convert to a list for the sake of printing. :)
I am a little bit confused,
def square(number):
return number*number
even_numbers = [2,4]
correct me if I am wrong:
even_numbers_squared should be = [4, 16] instead of [4, 8]
Yupp you're correct! Just absent-mindedley added there instead apparently lol.