How to Concatenate Two or Multiple Lists in Python

  1. + to Concatenate Two Lists in Python
  2. += to Concatenate Two Lists in Place in Python
  3. itertools.chain Method Concatenate Two Lists in Python
  4. Extend() Method to Concatenate Lists in Python
  5. [*A, *B] Unpacking Method in Python List Concatenation
  6. Conclusion

This article introduces how to concatenate two lists in Python.

Concatenation is the operation to join two lists together, or in other words, the elements of the second list are appended to the end of the first list.

Suppose we have two lists to be concatenated,

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

We could have multiple methods to concatenate lists, but they differ in the performance when the length increases or the number of concatenated lists increases.

We will introduce these different methods and give you a performance comparison.

+ to Concatenate Two Lists in Python

+ operator could concatenate two lists and return a new list.

>>> list1 = [1, 2, 3, 4]
>>> list2 = [5, 6, 7, 8]
>>> result = list1 + list2
>>> result
[1, 2, 3, 4, 5, 6, 7, 8]

You could join multiple lists by using the + operator. See the example below.

>>> list1 = [1, 2, 3, 4]
>>> list2 = [5, 6, 7, 8]
>>> list3 = [11, 12, 13, 14]
>>> result = list1 + list2 + list3
>>> result
[1, 2, 3, 4, 5, 6, 7, 8, 11, 12, 13, 14]

+= to Concatenate Two Lists in Place in Python

+= is similar to the above method, but it will change the first list’s data in place.

>>> list1 = [1, 2, 3, 4]
>>> list2 = [5, 6, 7, 8]
>>> list1 += list2
>>> list1
[1, 2, 3, 4, 5, 6, 7, 8]

itertools.chain Method Concatenate Two Lists in Python

chain from itertools module treats consecutive sequences as one single sequence,

>>> list1 = [1, 2, 3, 4]
>>> list2 = [5, 6, 7, 8]
>>> import itertools
>>> result = list(itertools.chain(list1, list2))
[1, 2, 3, 4, 5, 6, 7, 8]
Information

itertools.chain has an alternate constructor - intertools.chain.from_iterable(). It has a single iterable argument whose input is evaluated lazily.

Extend() Method to Concatenate Lists in Python

The list built-in extend() method extends a list by appending elements from the iterable.

>>> list1 = [1, 2, 3, 4]
>>> list2 = [5, 6, 7, 8]
>>> list1.extend(list2)
>>> list1
[1, 2, 3, 4, 5, 6, 7, 8]

It also changes the data of the existing list in-place rather than returning a new list.

[*A, *B] Unpacking Method in Python List Concatenation

Additional unpacking like * for filterable unpacking operator and ** for dictionary unpacking operator are extended from Python 3.5, as explained in PEP-0448.

>>> list1 = [1, 2, 3, 4]
>>> list2 = [5, 6, 7, 8]
>>> result = [*list1, list2]
>>> result
[1, 2, 3, 4, 5, 6, 7, 8]

This method couldn’t apply to the case of N lists unless you would like to unpack each list manually.

Warning

Iterable unpacking cannot be used in the list comprehension, as demonstrated below.

>>> A = [1,2,3]
>>> B = [4,5,6]
>>> C = [7,8,9]
>>> [*t for t in [A,B,C]]
SyntaxError: iterable unpacking cannot be used in comprehension

Conclusion

Method Version In-place?
a + b - No
list(intertools.chain(a,b)) >=2.3 No
[*a, *b] >=3.5 No
a += b - Yes
a.extend(b) - Yes

We use perfplot module to compare the performance of the above methods.

Python List Concatenation Methods Performance Comparison

As shown in the graph, a.extend(b) and a+b methods are almost the same in the performance, and the best among all the methods. list(chain(a,b)) method has the worst performance.

Related Article - Python List

  • How to Create a List With a Specific Size in Python
  • How to Deduplicate a List in Python
  • comments powered by Disqus