Ways to Combine Lists in Python

Updated: June 6, 2023 By: Frienzied Flame Post a comment

This succinct and practical article walks you through a couple of different ways to combine multiple lists in Python. Each approach will be accompanied by a clear illustrative example. Without any further ado, let’s get started.

Using the + operator

This might be the most straightforward and intuitive method. Other than Python, not many programming languages have a simple syntax like this.

Example:

list1 = [1, 2, 3, 4, 5]
list2 = [6, 7, 8, 9, 10]

combined_list = list1 + list2
print(combined_list)

Output:

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

Another example:

group1 = ["John", "Bob", "Alice"]
group2 = ["Mary", "Jane", "Alice"]
group3 = ["Sling Academy"]

combined_groups = group1 + group2 + group3
print(combined_groups)

Output:

['John', 'Bob', 'Alice', 'Mary', 'Jane', 'Alice', 'Sling Academy']

Using the unpacking operator (*)

Since Python 3.5, you can use the * operator to unpack values from lists and other data structures like dictionaries or tuples (this is quite similar to the spread operator in Javascript and some other programming languages). We can also take advantage of this succinct syntax to join multiple lists together.

Example:

animals = ["dog", "cat", "bird"]
vehicles = ["car", "bike", "plane"]
fruits = ["apple", "banana", "orange"]
flowers = ["rose", "lily", "daisy"]

things = [*animals, *vehicles, *fruits, *flowers]
print(things)

Output:

['dog', 'cat', 'bird', 'car', 'bike', 'plane', 'apple', 'banana', 'orange', 'rose', 'lily', 'daisy']

Using the extend() method

You can use the List.extend() method to concatenate 2 Python lists. It will add all elements from a list to a given list. If you want the original lists intact, you can create a new empty list the call the extend() method on this one. This is exactly what the example below does.

Example:

list1 = ["a", "b" , "c"]
list2 = ["d", "e", "f"]
list3 = ["g", "h", "i"]

joined_list = []
joined_list.extend(list1)
joined_list.extend(list2)
joined_list.extend(list3)

print(joined_list)

Output:

['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i']

Using the itertools.chain() function

Another solution to concatenate Python lists.

Example:

import itertools
list1 = [1, 2, 3]
list2 = [4, 5, 6]
list3 = [7, 8, 9]
combined_list = list(itertools.chain(list1, list2, list3))

print(combined_list)

Output:

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