The built-in data type List stores multiple items in a single variable. You can create lists using square brackets []. If you have two lists and you want to concatenate them, there are several methods you can use to do so.
This tutorial will go through six of the concatenation methods with examples. We will also go through how to concatenate lists without duplicates.
Table of contents
Method #1: Using the + Operator
The most straightforward method to perform list concatenation is the “+” operator. This operator adds an entire list to the other list with one line of code. Let’s look at an example of using the + operator.
list_1 = [2, 4, 6, 8, 10]
list_2 = [5, 15, 45, 135, 405]
list_3 = list_1 + list_2
print(f'Concatenating two lists using the + operator: {list_3}')
The above code defines two lists with five elements inside each of them. We then define a third list variable and assign the concatenation of the first two lists to it. The program then prints the third list contents to the console. Let’s see what happens when we run the code.
Concatenating two lists using the + operator: [2, 4, 6, 8, 10, 5, 15, 45, 135, 405]
We can concatenate any number of lists using the + operator. Let’s look at an example of concatenating three lists:
list_1 = [2, 4, 6, 8, 10]
list_2 = [5, 15, 45, 135, 405]
list_3 = [4, 16, 64, 256, 1024]
list_4 = list_1 + list_2 + list_3
print(f'Concatenating three lists using + operator: {list_4}')
Concatenating three lists using + operator: [2, 4, 6, 8, 10, 5, 15, 45, 135, 405, 4, 16, 64, 256, 1024]
Method #2: Using append()
You can concatenate lists using the append() method. To do this, we will iterate over one of the lists using a for loop. Each iteration will append a value to the other list in ascending index order. Then, we will print the joined lists to the console. Let’s look at an example of the append() method with two lists.
list_1 = [2, 4, 6, 8, 10]
list_2 = [5, 15, 45, 135, 405]
for i in list_2:
list_1.append(i)
print(f'Concatenating two lists using append(): {list_1}')
In the above code, we use for… in to access each element in list_2. Let’s see what we get when we run the code:
Concatenating two lists using append(): [2, 4, 6, 8, 10, 5, 15, 45, 135, 405]
We can use the append() method for more than two lists by defining more than additional for loops. Let’s look at an example with three lists:
list_1 = [2, 4, 3, 5, 5]
list_2 = [4, 5, 5, 6, 7]
list_3 = [7, 6, 10, 34, 21]
for i in list_2:
list_1.append(i)
for i in list_3:
list_1.append(i)
print(f'Concatenating three lists using append(): {list_1}')
The above code produces the following result:
Concatenating three lists using append(): [2, 4, 3, 5, 5, 4, 5, 5, 6, 7, 7, 6, 10, 34, 21]
Method #3: Using List Comprehension
List comprehension provides concise syntax to create a new list based on the values of an existing list or several existing lists. Let’s look at an example of a list comprehension to concatenate two lists.
list_1 = [2, 4, 6, 8, 10]
list_2 = [5, 15, 45, 135, 405]
result_list = [y for x in [list_1, list_2] for y in x]
print(f'Concatenating two lists using list comprehension: {result_list}')
Running the above code produces the following result:
Concatenating two lists using list comprehension: [2, 4, 6, 8, 10, 5, 15, 45, 135, 405]
A better solution for concatenating more than two lists is to use itertools.chain(), which we will go through in the next example.
Method #4: Using itertools.chain()
The itertools module contains a collection of functions for handling iterators. With ease, you can use these functions to iterate over iterables like lists or strings. The itertools function chain() groups a series of iterables and returns one iterable. We cannot use the output directly, and therefore we need to convert the output of the chain() function back to a list. Let’s look at an example where we import itertools and use the chain() function to concatenate two lists.
import itertools
list_1 = [2, 4, 6, 8, 10]
list_2 = [5, 15, 45, 135, 405]
result_list = list(itertools.chain(list_1, list_2))
print(f'Concatenating two lists using itertools.chain(): {result_list}')
Let’s run the code to get the output.
Concatenating two lists using itertools.chain(): [2, 4, 6, 8, 10, 5, 15, 45, 135, 405]
We can also use the chain() function for more than two lists. Let’s look at an example with three lists.
import itertools
list_1 = [2, 4, 3, 5, 5]
list_2 = [4, 5, 5, 6, 7]
list_3 = [7, 6, 10, 34, 21]
list_4 = list(itertools.chain(list_1, list_2, list_3))
print(f'Concatenating three lists using itertools.chain(): {list_4}')
Let’s run the code to get the result:
Concatenating three lists using itertools.chain(): [2, 4, 3, 5, 5, 4, 5, 5, 6, 7, 7, 6, 10, 34, 21]
The itertools.chain() function is handy for efficiently concatenating large lists. We can also use the chain() function on numpy arrays. For further reading on concatenating NumPy arrays correctly, go to the article: How to Solve Python TypeError: only integer scalar arrays can be converted to a scalar index.
Method #5: Using extend()
The built-in method extend() concatenates lists by adding the whole of one list to the end of the other list. Let’s look at an example of concatenating two lists using extend().
list_1 = [2, 4, 6, 8, 10]
list_2 = [5, 15, 45, 135, 405]
list_1.extend(list_2)
print(f'Concatenating two lists using list.extend(): {list_1}')
Running the code gives us the following result:
Concatenating two lists using list.extend(): [2, 4, 6, 8, 10, 5, 15, 45, 135, 405]
If we want to concatenate more than two lists, we must use the extend() method multiple times. Let’s look at an example of using extend() for three lists.
list_1 = [2, 4, 3, 5, 5]
list_2 = [4, 5, 5, 6, 7]
list_3 = [7, 6, 10, 34, 21]
list_1.extend(list_2)
list_1.extend(list_3)
print(f'Concatenating three lists using extend(): {list_1}')
Note that we call extend() on the first list twice. Let’s see what happens when we run the code:
Concatenating three lists using extend(): [2, 4, 3, 5, 5, 4, 5, 5, 6, 7, 7, 6, 10, 34, 21]
Method #6: Using the * Operator (Python version 3.6+)
The * operator works the same way as the + operator, and we can use it to concatenate two or more lists. The * operator is usable for Python 3.6+ versions. You can check your Python version using the methods described in the article: “How to Check Python Version for Linux, Mac, and Windows“. Let’s look at an example of concatenating two lists using the * operator.
list_1 = [2, 4, 6, 8, 10]
list_2 = [5, 15, 45, 135, 405]
result_list = [*list_1, *list_2]
print(f'Concatenating two lists using * operator: {result_list}')
The above code produces the following output:
Concatenating two lists using * operator: [2, 4, 6, 8, 10, 5, 15, 45, 135, 405]
We can use the * operator for concatenating more than two lists. Let’s look at an example with three lists:
list_1 = [2, 4, 3, 5, 5]
list_2 = [4, 5, 5, 6, 7]
list_3 = [7, 6, 10, 34, 21]
list_4 = [*list_1, *list_2, *list_3]
print(f'Concatenating three lists using * operator: {list_4}')
Let’s run the code to see what happens:
Concatenating three lists using * operator: [2, 4, 3, 5, 5, 4, 5, 5, 6, 7, 7, 6, 10, 34, 21]
Method #7: Concatenate Lists Without Duplicates Using set()
When performing concatenation, we may want to remove duplicates from the list. The previous examples use methods that retain duplicate elements. The most efficient way to remove duplicates from a list is to convert the final list to a set and then convert it back to a list. Let’s look at an example of two lists with some common elements.
list_1 = [2, 4, 3, 5, 5]
list_2 = [4, 5, 5, 6, 7]
list_3 = [7, 6, 10, 34, 21]
list_4 = list_1 + list_2 + list_3
print(f'Concatenating three lists using + operator: {list_4}')
The above code concatenates three lists using the + operator. Let’s see what happens when we run the code:
Concatenating three lists using + operator: [2, 4, 3, 5, 5, 4, 5, 5, 6, 7, 7, 6, 10, 34, 21]
Let’s get rid of the duplicates by converting list_4 into a set, then back to a list.
deduplicated_list = list(set(list_4))
print(f'Final list with duplicates removed from list: {deduplicated_list}')
Let’s see what happens when we run the code:
Final list with duplicates removed from list: [2, 3, 4, 5, 6, 7, 34, 10, 21]
We can see that the final list does not contain any of the duplicate elements present in list_4.
Summary
Congratulations on reading to the end of this tutorial! Concatenation is a valuable operation in Python, and there are several ways to perform it. The simplest way to perform concatenation is to use the + operator or the * operator. The * operator is present in all Python versions from 3.6. We can use set() to remove duplicates from a concatenation of lists. The itertools.chain() function is helpful for large lists, and we can also use it for numpy arrays. Now you are ready to join lists together like a pro!
For further reading on manipulation of lists, go to the articles:
- How to Calculate the Cartesian Product in Python.
- How to Remove Empty Strings from a List of Strings in Python
Go to the online courses page on Python to learn more about coding in Python for data science and machine learning.
Have fun and happy researching!
Suf is a senior advisor in data science with deep expertise in Natural Language Processing, Complex Networks, and Anomaly Detection. Formerly a postdoctoral research fellow, he applied advanced physics techniques to tackle real-world, data-heavy industry challenges. Before that, he was a particle physicist at the ATLAS Experiment of the Large Hadron Collider. Now, he’s focused on bringing more fun and curiosity to the world of science and research online.