How to concatenate Lists In Python?

In this tutorial, we are going to learn how to add / concatenate two lists in Python.

What Is Concatenation?
Concatenation of lists is an operation where the elements of one list are added at the end of another list. For example, if we have a list with elements [1, 2, 3] and another list with elements [4, 5, 6]. If we concatenate both the lists, then, the result may be [1, 2, 3, 4, 5, 6] or [4, 5, 6, 1, 2, 3] based on the order we concatenate.

There are different ways to concatenate lists in Python. We are going to look at the following ways one by one.

  • Concatenation Operator(+)
  • Loops
  • List Comprehensions
  • * Operator(Unpacking)
  • extend() Built-in Method
  • itertools.chain()

Let’s see all the ways to concatenate lists.

1. Concatenation Operator(+)

1.1 It’s a general way to concatenate two lists in Python. Most of the people use + operator to concatenate the lists. Look at the example below to understand it clearly.


# initializing lists
list_one = [0, 1, 2, 3, 4]
list_two = [5, 6, 7, 8, 9]

# add two lists using + operator
result = list_one + list_two

print(result)

Output


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

1.2 We can concatenate any number of lists, we want to use the concatenation operator +. See one example, which concatenates more than two lists at a time using the + operator.


# initializing lists
list_one = [1, 2, 3]
list_two = [4, 5, 6]
list_three = [7, 8, 9]

# concatenating two lists using + operator
result = list_one + list_two + list_three

print(result)

Output


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

2. Loops

2.1 The next way to concatenate lists is using the loops. We will iterate through one of the lists and append its elements to another list. At the end of the loop, one of the lists will contain all the elements. Let’s write a loop which concatenates two lists.


# initializing lists
list_one = [0, 1, 2, 3, 4]
list_two = [5, 6, 7, 8, 9]

# appending elements of list_two at the end of the list_one
# at the end of the loop list_one will be [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
for element in list_two:
    list_one.append(element)

print(list_one)

Output


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

2.2 We can also create a new list with the elements from both lists. The result will be the same in both cases.


# initializing lists
list_one = [0, 1, 2, 3, 4]
list_two = [5, 6, 7, 8, 9]

# empty list to store the elements from both lists
result = []

# appending elements of list_two at the end of the list_one
# at the end of the loop list_one will be [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
for element in list_one:
    result.append(element)

for element in list_two:
    result.append(element)

print(result)

Output


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

2.3 We can concatenate more than two lists at a time using the loop if we want to. Modify the code and achieve the result. Follow the same procedure as we follow to concatenate two lists in the previous example. Examine the below code for reference.


# initializing lists
list_one = [1, 2, 3]
list_two = [4, 5, 6]
list_three = [7, 8, 9]

# empty list to store the elements from both lists
result = []

# appending elements of list_two at the end of the list_one
# at the end of the loop list_one will be [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
for element in list_one:
    result.append(element)

for element in list_two:
    result.append(element)

for element in list_three:
    result.append(element)

print(result)

Output


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

If you want to append elements without many loops, then, you have to use an inner loop. You can try it yourself as an exercise. Let’s move to see the following way.

3. List Comprehensions

3.1 I think all of you know familiar with the list comprehensions. If you don’t know list comprehension concept in Python, read it first. In simple words, list comprehensions are used to create lists using for loop in a different way. Let’s see how to concatenate two lists using the list comprehensions.


# initializing lists
list_one = [0, 1, 2, 3, 4]
list_two = [5, 6, 7, 8, 9]

# concatenating using the list comprehension
result = [element for lis in [list_one, list_two] for element in lis]

print(result)

Output


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

3.2 If you want to concatenate more than two lists in this way, then, add the list name to the *list comprehension*. Let’s see it with an example.


# initializing lists
list_one = [1, 2, 3]
list_two = [4, 5, 6]
list_three = [7, 8, 9]

# concatenating using the list comprehension
result = [element for lis in [list_one, list_two, list_three] for element in lis]

print(result)

Output


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

4. * Operator (Unpacking)

4.1 If you are familiar with the unpacking of elements from the iterators using operator then, you may know this way of concatenating. One of the primary use of is to unpack the elements from an iterator. Unpacking refers to exploring every element of an iterator. See a sample example of unpacking.


# initializing list
list = [1, 2, 3]

# general way of printing a list
print(list)

# unpacking the list using *
print(*list)

Output


[1, 2, 3]
1 2 3

4.2 Hope you understand the unpacking using . Now we will see how to concatenate two lists using the operator.


# initializing lists
list_one = [0, 1, 2, 3, 4]
list_two = [5, 6, 7, 8, 9]

# concatenating two lists using *
result = [*list_one, *list_two]

print(result)

Output


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

4.3 We can concatenate more list by passing them to the [] with an * operator. Most of the people don’t use this way to concatenate the lists. It’s the best way for me to concatenate.


# initializing lists
list_one = [1, 2, 3]
list_two = [4, 5, 6]
list_three = [7, 8, 9]

# concatenating lists using *
result = [*list_one, *list_two, *list_three]

print(result)

Output


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

5. extend() Built-in Method

5.1 The extend() is a built-in list method to extend {In general concatenating} the lists. See the below example.


# initializing lists
list_one = [0, 1, 2, 3, 4]
list_two = [5, 6, 7, 8, 9]

# concatenating using the extend() method
list_one.extend(list_two) # list_one contains all the elements

print(list_one)

Output


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

5.2 We can also do it for more than two lists. The problem here is that we have to extend the lists one by one in different lines of code.


# initializing lists
list_one = [1, 2, 3]
list_two = [4, 5, 6]
list_three = [7, 8, 9]

# concatenating lists using extend() method
list_one.extend(list_two)

list_one.extend(list_three) # now, list_one contains all the elements

print(list_one)

Output


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

6. itertools.chain()

6.1 Last and least used method to concatenate lists is itertools.chain(). The itertools.chain() method returns a chain objects which is a generator. We have to convert the resultant chain object to a list. See the example to get a clear idea.


# importing the itertools module for chain() method
import itertools

# initializing lists
list_one = [0, 1, 2, 3, 4]
list_two = [5, 6, 7, 8, 9]

# concatinating two lists using the itertools.chain() method
print(list(itertools.chain(list_one, list_two)))

Output


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

6.2 Pass more than two lists to the itertools.chain() method to concatenate all the lists.


# importing the itertools module for chain() method
import itertools

# initializing lists
list_one = [1, 2, 3]
list_two = [4, 5, 6]
list_three = [7, 8, 9]

# concatenating lists using itertools.chain() method
print(list(itertools.chain(list_one, list_two, list_three)))

Output


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

Conclusion

Best and efficient ways to concatenate lists are used + and * operators. You can use any of the above ways to concatenate lists. In practice, you will find out when to use, which method based on the need of the program.

The result of the concatenation will be based on the order you place the lists to concatenate. Hope you understand the different ways to concatenate lists. Happy Coding :)

References

About the Author

author image
Hafeezul Kareem
I am a Pythoneer currently studying 3rd year in my Computer Science course. I will be a computer science graduate in 2 years. I am very fond of Python and Programming. I love to learn new technologies and enthusiastic about sharing my knowledge across the programming community.

Comments

avatar