r/pythonhelp • • Dec 14 '17

Merging 2 lists of same length

list1 = ["a","b"] list2 = ["c","d"]

<some magic here>

out:

["ac","bd"]

3 Upvotes

1 comment sorted by

1

u/ojiisan Jan 17 '18 edited Jan 17 '18
map(''.join, zip(list1, list2))

The zip function takes the two lists and creates a new list of 2-tuples that contain pairs of elements (in order) from the original lists:

[("a", "c"), ("b", "d")]

which isn't quite what you want. So the map function then takes the output from zip and applies the join function (with an empty string as the joining item) to each of the tuples, resulting in the merged list you were looking for.