# list 如何在Python中連接兩個列表？

``````import itertools
for item in itertools.chain(listone, listtwo):
# do something with each list item
``````
python list

``````listone = [1, 2, 3]
listtwo = [4, 5, 6]
``````

``````>>> joinedlist
[1, 2, 3, 4, 5, 6]
``````

``````listone = [1,2,3]
listtwo = [4,5,6]
mergedlist = []
mergedlist.extend(listone)
mergedlist.extend(listtwo)
``````

``````>>> listone = [1,2,3]
>>> listtwo = [4,5,6]
>>>
>>> listone + listtwo
[1, 2, 3, 4, 5, 6]
``````

``````a = [[1,2,3], [4,5,6], [7,8,9]]
reduce(lambda c, x: c + x, a, [])
``````

``````[1, 2, 3, 4, 5, 6, 7, 8, 9]
``````

``````a = [[1,2,3], [4,5,6], [7,8,9]]
list(itertools.chain(*a))
``````

Patrick Collins在評論中提出的替代方案也適用於您：

``````sum(a, [])
``````

``````listone = [1,2,3]
listtwo = [4,5,6]

def merge(l1, l2):
yield from l1
yield from l2

>>> list(merge(listone, listtwo))
[1, 2, 3, 4, 5, 6]
``````

``````def merge(*iters):
for it in iters:
yield from it

>>> list(merge(listone, listtwo, 'abcd', [20, 21, 22]))
[1, 2, 3, 4, 5, 6, 'a', 'b', 'c', 'd', 20, 21, 22]
``````

``````listone = [1,2,3]
listtwo = [4,5,6]

print(result)

>>> [1, 2, 3, 4, 5, 6]
``````

``````import operator

listone = [1,2,3]
listtwo = [4,5,6]

print(result)

>>> [1, 2, 3, 4, 5, 6]
``````

``````>>> a = [1, 2, 3, 4]
>>> b = [1, 4, 6, 7]
>>> c = a + b
>>> c
[1, 2, 3, 4, 1, 4, 6, 7]
``````

``````>>> a = [1, 2, 3, 4, 5, 6]
>>> b = [5, 6, 7, 8]
>>> c = list(set(a + b))
>>> c
[1, 2, 3, 4, 5, 6, 7, 8]
``````

``````for it in iterables:
for element in it:
yield element
``````

（請參閱https://docs.python.org/2/library/itertools.html ），所以我從這裡獲得了靈感，並沿著這些方向寫了一些內容：

``````for iterable, header, flag in ( (newList, 'New', ''), (modList, 'Modified', '-f')):
for path in iterable:
[...]
command = 'cp -r' if os.path.isdir(srcPath) else 'cp'
print >> SCRIPT , command, flag, srcPath, mergedDirPath
[...]
``````

``````list(set(listone) | set(listtwo))
``````

``````lst1 = [1,2]

lst2 = [3,4]

def list_combinationer(Bushisms, are_funny):

for item in lst1:
lst2.append(item)
lst1n2 = sorted(lst2)
print lst1n2

list_combinationer(lst1, lst2)

[1,2,3,4]
``````

``````joinedList = []
for i in listOne:
joinedList.append(i)
for j in listTwo:
joinedList.append(j)

sorted(joinedList)

return joinedList
``````

``````li = [[1,2,3], [4,5,6], [7,8,9]]
``````

``````[1, 2, 3, 4, 5, 6, 7, 8, 9]
``````

python   list