# list python用法 - 如何在Python中連接兩個列表？

matplotlib教學 matplotlib刻度 (21)

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

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

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

``````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]
``````

``````list1 = [1,2,5]
list2 = [2,3,4]
newlist = []

while list1 and list2:
if list1[0] == list2[0]:
newlist.append(list1.pop(0))
list2.pop(0)
elif list1[0] < list2[0]:
newlist.append(list1.pop(0))
else:
newlist.append(list2.pop(0))

if list1:
newlist.extend(list1)
if list2:
newlist.extend(list2)

assert(newlist == [1, 2, 3, 4, 5])
``````

``````>>> l1 = ['a']; l2 = ['b', 'c']; l3 = ['d', 'e', 'f']
>>> [i for i in itertools.chain(l1, l2)]
['a', 'b', 'c']
>>> [i for i in itertools.chain(l1, l2, l3)]
['a', 'b', 'c', 'd', 'e', 'f']
``````

``````>>> il = [['a'], ['b', 'c'], ['d', 'e', 'f']]
>>> [i for i in itertools.chain.from_iterable(il)]
['a', 'b', 'c', 'd', 'e', 'f']
``````

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

sorted(joinedList)

return joinedList
``````

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

``````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]
``````

``````>>> 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]
``````

``````l1 = [1,2,3]
l2 = [4,5,6]
l1.extend(l2)
print l1
``````

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

mergedlist = listone + listtwo
``````

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

``````from heapq import merge

a = [1,2,4]
b = [2,4,6,7]

print list(merge(a,b))
``````

``````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
[...]
``````

``````import itertools

A = list(zip([1,3,5,7,9],[2,4,6,8,10]))
B = [1,3,5,7,9]+[2,4,6,8,10]
C = list(set([1,3,5,7,9] + [2,4,6,8,10]))

D = [1,3,5,7,9]
D.append([2,4,6,8,10])

E = [1,3,5,7,9]
E.extend([2,4,6,8,10])

F = []
for a in itertools.chain([1,3,5,7,9], [2,4,6,8,10]):
F.append(a)

print ("A: " + str(A))
print ("B: " + str(B))
print ("C: " + str(C))
print ("D: " + str(D))
print ("E: " + str(E))
print ("F: " + str(F))
``````

``````A: [(1, 2), (3, 4), (5, 6), (7, 8), (9, 10)]
B: [1, 3, 5, 7, 9, 2, 4, 6, 8, 10]
C: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
D: [1, 3, 5, 7, 9, [2, 4, 6, 8, 10]]
E: [1, 3, 5, 7, 9, 2, 4, 6, 8, 10]
F: [1, 3, 5, 7, 9, 2, 4, 6, 8, 10]
``````

``````mergedlist = list(set(listone + listtwo))
``````

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

``````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]
mergedlist = []
mergedlist.extend(listone)
mergedlist.extend(listtwo)
``````

``````a = [1, 2, 3]
b = [4, 5, 6]

c = a + b
``````

``````c = []
a = [1, 2, 3]
b = [4, 5, 6]

c += (a + b)
``````

``````c = list(set(a + b))
``````

``````>>> l=[[1, 2, 3], [4, 5, 6], [7, 8, 9]]
>>> import itertools
>>> list(itertools.chain.from_iterable(l))
[1, 2, 3, 4, 5, 6, 7, 8, 9]
``````

1.請注意， `chain.from_iterable()`在python => 2.6中可用。在其他版本中，使用`chain(*l)`

``````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]
``````

``````class Person(models.Model):
first_name = models.CharField(max_length=30)
last_name = models.CharField(max_length=30)
``````