I want to merge 3 lists in to a single list. For example, I have three lists:
a = [0, 3, 6, 9]
b = [1, 4, 7, 10]
c = [2, 5, 8, 11]
and fina
a = [0, 3, 6, 9]
b = [1, 4, 7, 10]
c = [2, 5, 8, 11]
d=[]
print [j for i in zip(a,b,c) for j in i]
Output:[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]
import itertools as it
list(it.chain.from_iterable(it.izip(a,b,c)))
Using reduce is another option:
>>> a = [0, 3, 6, 9]
b = [1, 4, 7, 10]
c = [2, 5, 8, 11]
>>> reduce(lambda x, y: list(x)+list(y), zip(a,b, c))
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]
You can do it like this:
a = [0, 3, 6, 9]
b = [1, 4, 7, 10]
c = [2, 5, 8, 11]
merged=a+b+c
merged.sort()
Since you are adding the list the merged list will contains all values from abc but not in the correct order.That's why I used .sort() to sort the list