Python How to pair two list by lambda and map

Viewed 45822

For example, I have following two lists

listA=['one', 'two' , 'three'] listB=['apple','cherry','watermelon']

How can I pair those two lists to get this output, using map and lambda?

one apple
two cherry
three watermelon

I know how to do it by the list comprehension,

[print(listA[i], listB[i]) for i in range(len(listA))]

but I can't figure out a map and lambda solution. Any ideas?

6 Answers

Let's consider there are two lists such as list1,list2. we can pair them in the list or tuple type.

list1=['1', '2' , '3']
list2=['3','2','1']
output = list (map (  lambda x,y: [x,y], list1,list2    ))
print(output)

Output:

[['1', '3'], ['2', '2'], ['3', '1']]
Related