How to Concatenate Two Lists in Python

Almost every Python developer heavily relies on lists to store and manipulate data. Sometimes they may need to join two or more lists in Python. This is often required if you want to combine two or more data sets into one for further processing. There are several ways to combine lists in Python. In this article, we will learn how to concatenate two lists in Python.

How to Concatenate Two Lists in Python

Here are the different ways to concatenate two lists in Python.

1. Using + operator

Python provides ‘+’ operator to concatenate two or more lists. Please note, it creates a new list as a result and does not modify any of the existing lists. Here is its syntax.

list1 + list2 + ...

Here is an example to combine two lists using this operator.

a = [1, 2, 3]
b = [4, 5, 6]
c = a + b
print(c) # output is [1, 2, 3, 4, 5, 6]

You can also use it to join more than 2 lists.

a = [1, 2, 3]
b = [4, 5, 6]
c = [7, 8, 9]
d = a + b + c
print(d) # output is [1, 2, 3, 4, 5, 6, 7, 8, 9]

Please note, the ‘+’ operator copies only the shallow copies of each list. If your list contains simple data types such as numbers and strings, this will work as it is. If you need to use deep copies of the lists, then you can call copy.deepcopy() function on the list before concatenation.

import copy
a = [1, 2, 3]
b = [4, 5, 6]
c = copy.deepcopy(a) + copy.deepcopy(b)
print(c) # output is [1, 2, 3, 4, 5, 6]

This operator is available in every Python version and has not restrictions. You can easily use it to concatenate two or more lists.

Also, please note, if there are any duplicate elements in the list, they will be included as it is in the result, without deduplication.

a = [1, 2, 3]
b = [1, 2, 3]
c = a + b
print(c) # output is [1, 2, 3, 4, 5, 6]

2. Using += operator

In the above example, the ‘+’ operator creates a new list to store the result. If you want to concatenate one of the two lists to the other list, then you can use += operator instead. It will modify the resultant list without creating a new one. This is useful if your lists are very big and you do not want to store the result in a new list.

a = [1, 2, 3]
b = [4, 5, 6]
b += a
print(b) # output is [1, 2, 3, 4, 5, 6]

As you can see above, list b is modified while list a remains unchanged. No new list is created.

This operator is also available in every Python version. But it works on only two lists at a time.

3. Using chain() function

So far, we have seen examples where the result of list concatenation is stored in a new or one of the two lists. If you are working with large lists, this can occupy a lot of space and the operation itself can be very slow. In such cases, you may want to use itertools. It is a powerful Python library that offers many useful functions to work with iterables such as lists. It provides a function called chain() that is used to concatenate iterables such as lists. It returns an iterator to a generator object instead of returning a list of final output. You can loop through this iterable to get items of concatenated list. Here is an example.

import itertools
a = [1, 2, 3]
b = [4, 5, 6]
c = itertools.chain(a,b) # output is [1, 2, 3, 4, 5, 6]

for i in c:
print(i)

Here is its output.

1
2
3
4
5
6

In the above code, we first import itertools. Then we call chain() function on the two lists. This returns an iterator object to the concatenated list. We loop through this list to get each item of the concatenated list.

If you print the result c directly, you will see that it is an iterator and not a list.

import itertools
a = [1, 2, 3]
b = [4, 5, 6]
c = itertools.chain(a,b)
print(c) # output is <itertools.chain object at 0x7a03c8c2d4b0>

Please note, itertools is available in Python >= 2.3.

4. Using Unpacking Operator

If you use Python >= 3.5, you can use unpacking operator for this purpose. It is very convenient and can be used to combine two or more iterables such as lists, tuples, etc. It is applied by prefixing the iterable with a * operator. Here is its syntax to concatenate lists/

[*list1, *list2, ...]
a = [1, 2, 3]
b = [4, 5, 6]
c = [*a, *b]
print(c) # output is [1, 2, 3, 4, 5, 6]

You can also use it to join more than 2 lists at a time.

a = [1, 2, 3]
b = [4, 5, 6]
c = [7, 8, 9]
d = [*a, *b, *c]
print(d) # output is [1, 2, 3, 4, 5, 6, 7, 8, 9]

Please note, this method also creates a new list to store the result without changing the original lists.

5. Using Extend() function

Every list also supports built-in function extend() that allows you to easily combine one list into another. It is also available in all Python versions. It modifies the target list while leaving the other list unchanged.

a = [1, 2, 3]
b = [4, 5, 6]

a.extend(b)
print(a) # output is [1, 2, 3, 4, 5, 6]

Conclusion

In this article, we have learnt many different ways to concatenate lists in Python. You can use any of these methods as per your requirement. + and += operators work on all Python versions. The former operator creates a new list whereas the latter modifies one of the existing lists. Itertools.chain() function is available in Python 2.3 and returns an iterator to the concatenated list.

Also read:

How to List All Files in Directory
How to Clone List in Python
How to Delete File or Folder in Python

Leave a Reply

Your email address will not be published. Required fields are marked *