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?
Using list comprehension and zip:
Output:
You can use
zip
like below:specifically using map and lambda as asked...
though I'd probably break that up to make it more readable
EDITED - per comment below,
listCombined = list(zip(listA,listB))
was a wasteThe easiest solution would be to simply use
zip
as in:I guess it would be possible to use
map
and lambdas, but that would just needlessly complicate things as this is really the ideal case forzip
.Here what I got based on what you need (map and lambda),
Input:
Output: