Concatenate two lists in Python
This post provides an overview of some of the available alternatives to concatenate two lists in Python.
1. Using + operator
The + operator can be used to add two or more lists together and return a new list that contains all the elements of the original lists. This is the simplest way to concatenate two lists in Python. For example, the following code uses the + operator to add two lists together, resulting in a new list that contains all the elements of both lists.
|
1 2 3 4 5 |
firstlist = [1, 2, 3] secondlist = [4, 5, 6] joinedlist = firstlist + secondlist print(joinedlist) # prints [1, 2, 3, 4, 5, 6] |
Note that each time we use the + operator, a new list is created and all the elements of both lists are copied to it. Therefore, this method is not very efficient if we need to perform multiple concatenations.
2. Using operator.add() function
Alternatively, we can also use the add(x, y) function from the operator module for concatenating two lists, which is equivalent to the expression x + y.
|
1 2 3 4 5 6 7 |
import operator firstlist = [1, 2, 3] secondlist = [4, 5, 6] joinedlist = operator.add(firstlist, secondlist) print(joinedlist) # prints [1, 2, 3, 4, 5, 6] |
3. Using itertools.chain() function
Another option is to chain lists together using the chain() function of the itertools module. It returns an iterator of elements from the first iterable, followed by elements from the second iterable. This function can be used as:
|
1 2 3 4 5 6 7 |
import itertools firstlist = [1, 2, 3] secondlist = [4, 5, 6] joinedlist = list(itertools.chain(firstlist, secondlist)) print(joinedlist) # prints [1, 2, 3, 4, 5, 6] |
4. Using Iterable Unpacking Operator
Starting with Python 3.5, we can use the iterable unpacking operator (*) to join any number of lists. This operator was introduced with acceptance of PEP 448 — Additional Unpacking Generalizations and can be used to unpack an iterable object (such as another list) inside a list literal. A list literal is a way of creating a list by using square brackets and commas. For example, the following code uses the * operator to unpack two lists inside a list literal, resulting in a new list containing all the elements of both lists.
|
1 2 3 4 5 6 7 |
import itertools firstlist = [1, 2, 3] secondlist = [4, 5, 6] joinedlist = [*firstlist, *secondlist] print(joinedlist) # prints [1, 2, 3, 4, 5, 6] |
5. Using extend() function
The extend() function extends the list by appending all the elements of another iterable object (such as another list) to the end of an existing list. It can be used for the concatenation of two or more lists, as shown below:
|
1 2 3 4 5 6 7 8 |
firstlist = [1, 2, 3] secondlist = [4, 5, 6] joinedlist = [] joinedlist.extend(firstlist) joinedlist.extend(secondlist) print(joinedlist) # prints [1, 2, 3, 4, 5, 6] |
The above code adds all the elements of two lists to a new list while keeping the original lists intact. We can also use the extend() function to modify a list in place by appending another list to its end.
That’s all about concatenating two lists in Python.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)