+ 1
Challenge problem
Why does this code k = list(map(\ lambda x,y:x+y, list(range(100)),list(range(10)) )) print(k) Output : [0, 2,4,6,8,10,12,14,16,18] How does it really work?? Thanks đ
2 Answers
+ 8
The lambda takes a value from list( range( 100 ) ) and list( range( 10 ) ) and sum each taken value up.
But even though the first `list` has 100 elements, the lambda can only take 10 out of it because the second `list` only has that many elements.
list( range( 100 ) )
[ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, ... , 99 ]
list( range( 10 ) )
[ 0, 1, 2, 3, 4, 5 6, 7, 8, 9 ]
Lambda took a value from each list
0 and 0 => 0
1 and 1 => 2
2 and 2 => 4
....
9 and 9 => 18
I guess it goes like that ...
+ 3
Thanks Ipang