0

I'm trying to convert this list comprehension code to map and lambda code.

>>>list1 = [1,2,3]
>>>list2 = [10,20,30]
>>>print([m+n for m,n in zip(list1, list2)])
[11, 22, 33]

The code below is what I tried, but it shows TypeError

>>>print(list(map(lambda m,n:m+n, zip(list1, list2))))
Traceback (most recent call last):
  File "<console>", line 1, in <module>   
TypeError: <lambda>() missing 1 required positional argument: 'n'

I read that list comprehensions and labda function are interconvertible. Please point out my mistake!

1 Answer 1

2

lambda gets only one argument which is a tuple from zip that you need to unpack yourself:

>>> list1 = [1,2,3]
>>> list2 = [10,20,30]
>>> list(map(lambda x: x[0]+x[1], zip(list1, list2)))
[11, 22, 33]
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you niemmi! Now I can see the difference thanks to you! Have a good day!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.