How do I merge two lists into a single list?
Solution 1:
[j for i in zip(a,b) for j in i]
Solution 2:
If the order of the elements much match the order in your example then you can use a combination of zip and chain:
from itertools import chain
c = list(chain(*zip(a,b)))
If you don't care about the order of the elements in your result then there's a simpler way:
c = a + b
Solution 3:
Parsing
[item for pair in zip(a, b) for item in pair]
in your head is easy enough if you recall that the for
and if
clauses are done in order, followed a final append of the result:
temp = []
for pair in zip(a, b):
for item in pair :
temp.append(item )