Comment concrétiser deux listes en Python

Mots clés : pythonlistconcatenationpython

meilleur 5 Réponses Comment concrétiser deux listes en Python

vote vote

96

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

87

>>> l1 = [1, 2, 3] >>> l2 = [4, 5, 6] >>> joined_list = [*l1, *l2]  # unpack both iterables in a list literal >>> print(joined_list) [1, 2, 3, 4, 5, 6] 
l = [1, 2, 3] r = range(4, 7) res = l + r 
res = [*l, *r] 
vote vote

79

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

68

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

55

mergedlist = list(set(listone + listtwo)) 

Questions similaires