7 Ways To Combine Lists In Python

In this article, I’ll show you 7 different ways to combine lists in Python. I’m sure, there could be many more, but I found these ones the most frequently used ones.

Way 1: Using Extend

list1 = [1,2,3]
list2 = [4,5]
list1.extend(list2)
print(list1) 

#output ==> [1,2,3,4,5]

Way 2: Using +

list1 = [1,2,3]
list2 = [4,5]
list3 = list1 + list2
print(list3)

#output ==> [1,2,3,4,5]

Way 3: Using Sum(…)

list1 = [[1,2,3],[4,5]]
list2 = sum(list1,[])
print(list2)

#output ==> [1,2,3,4,5]

Way 4: Using Append(…)

list1 = [1,2,3]
list2 = [4,5]
for l in list2:
list1.append(l)
print(list1)

#output ==> [1,2,3,4,5]

Way 5: Using List Comprehension

list1 = [[1,2,3], [4,5]]
list2 = [item for childList in list1 for item in childList]
print(list2)

#output ==> [1,2,3,4,5]

Way 6: Using Itertools

import itertools
list1 = [[1,2,3], [4,5]]
list2 = list(itertools.chain.from_iterable(list1))
print(list2)

#output ==> [1,2,3,4,5]

Way 7: Using *

list1 = [1,2,3]
list2 = [4,5]
list3 = [*list1, *list2]
print(list3)

#output ==> [1,2,3,4,5]

Hope you enjoyed reading this article and learned at least one new way to combine lists.

Do not forget to check out my video portraying all of these ways here.


Similar Articles