Reputation: 5520
I just want to use zip
and map
to simultaneously iterate over 3 lists:
l0 = [0,0,0,0,0,0]
l1 = [1,2,3,4,5,6]
l2 = [2,3,4,5,6,7]
result = map(lambda x, y, z: x+y+z, zip(l0, l1, l2))
print(tuple(result))
but it returns an error:
Traceback (most recent call last):
File "pythontest.py", line 9, in <module>
print(tuple(result))
TypeError: <lambda>() missing 2 required positional arguments: 'y' and 'z'
Could anyone help?
Upvotes: 0
Views: 70
Reputation: 7844
You could use only one argument in your lambda
function, since zip
creates tuples of 3 elements, and then apply the sum
function:
result = map(lambda elem: sum(elem), zip(l0, l1, l2))
Upvotes: 2
Reputation: 472
The Problem is you are using the wrong variables... The zip returns a single element which contains the individual values.. So you need to access that by using the indices.
l0 = [0,0,0,0,0,0]
l1 = [1,2,3,4,5,6]
l2 = [2,3,4,5,6,7]
map(lambda x: x[0]+x[1]+x[2], zip(l0, l1, l2))
Out[23]: <map at 0x7fa48232f710>
result = map(lambda x: x[0]+x[1]+x[2], zip(l0, l1, l2))
print(tuple(result))
(3, 5, 7, 9, 11, 13)
Upvotes: 3