使用逐个元素将两个列表合并为一个

时间:2015-11-17 22:32:45

标签: python

如何使用逐个元素将两个列表合并为一个,如下所示:

list1 = ['a','c','e']

list2 = ['apple','carrot','elephant']

result = ['a', 'apple', 'c', 'carrot', 'e', 'elephant']

试用

result = [(x,y) for x,y in zip(list1,list2)]
print result

但是他们处于元组中,预期任何更容易的灵魂......

2 个答案:

答案 0 :(得分:7)

您可以使用双循环列表理解:

>>> list1 = ['a','c','e']
>>> list2 = ['apple','carrot','elephant']
>>> [x for z in zip(list1, list2) for x in z]
['a', 'apple', 'c', 'carrot', 'e', 'elephant']

答案 1 :(得分:3)

In [4]: list1 = ['a','c','e']

In [5]: list2 = ['apple','carrot','elephant']

In [6]: list(itertools.chain.from_iterable(zip(list1, list2)))
Out[6]: ['a', 'apple', 'c', 'carrot', 'e', 'elephant']