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 calling list(). I can see others have argued here that list() 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 call list() on the filter and map returns. Otherwise I get errors in the for loop (does not iterate in the loop without list() on filter, gives a TypeError: 'map' object is not subscriptable.
You set even_numbers_index = 0 and then in the for loop you set squared = even_numbers_squared[even_numbers_index] but you never update even_numbers_index so for each iteration in the loop you keep calling the same index number. When we extend the input list a bit to numbers = [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 and zip (and I really mean that. Short and well explained - thanks for that)
Hello, I'm a self-taught developer predominantly writing in Python & Javascript, here to write for beginners behind me, and for feedback from veterans ahead of me! I also love DevOps!
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.