How to Concatenate Lists in Python? [Interactive Guide]

So you have two or more lists and you want to glue them together. This is called list concatenation. How can you do that?

These are six ways of concatenating lists:

  1. List concatenation operator +
  2. List append() method
  3. List extend() method
  4. Asterisk operator *
  5. Itertools.chain()
  6. List comprehension
a = [1, 2, 3]
b = [4, 5, 6]

# 1. List concatenation operator +
l_1 = a + b

# 2. List append() method
l_2 = []

for el in a:
    l_2.append(el)
    
for el in b:
    l_2.append(el)


# 3. List extend() method
l_3 = []
l_3.extend(a)
l_3.extend(b)

# 4. Asterisk operator *
l_4 = [*a, *b]

# 5. Itertools.chain()
import itertools
l_5 = list(itertools.chain(a, b))

# 6. List comprehension
l_6 = [el for lst in (a, b) for el in lst]

Output:

'''
l_1 --> [1, 2, 3, 4, 5, 6]
l_2 --> [1, 2, 3, 4, 5, 6]
l_3 --> [1, 2, 3, 4, 5, 6]
l_4 --> [1, 2, 3, 4, 5, 6]
l_5 --> [1, 2, 3, 4, 5, 6]
l_6 --> [1, 2, 3, 4, 5, 6]
'''

What’s the best way to concatenate two lists?

If you’re busy, you may want to know the best answer immediately. Here it is:

  • To concatenate two lists l1, l2, use the l1.extend(l2) method which is the fastest and the most readable.
  • To concatenate more than two lists, use the unpacking (asterisk) operator [*l1, *l2, ..., *ln].

You’ll find all of those six ways to concatenate lists in practical code projects so it’s important that you know them very well. Let’s dive into each of them and discuss the pros and cons.

Related articles:

1. List Concatenation with + Operator

If you use the + operator on two integers, you’ll get the sum of those integers. But if you use the + operator on two lists, you’ll get a new list that is the concatenation of those lists.

l1 = [1, 2, 3]
l2 = [4, 5, 6]
l3 = l1 + l2
print(l3)

Output:

[1, 2, 3, 4, 5, 6]

The problem with the + operator for list concatenation is that it creates a new list for each list concatenation operation. This can be very inefficient if you use the + operator multiple times in a loop.

Performance:

How fast is the + operator really? Here’s a common scenario how people use it to add new elements to a list in a loop. This is very inefficient:

import time

start = time.time()

l = []
for i in range(100000):
    l = l + [i]

stop = time.time()

print("Elapsed time: " + str(stop - start))

Output:

Elapsed time: 14.438847541809082

The experiments were performed on my notebook with an Intel(R) Core(TM) i7-8565U 1.8GHz processor (with Turbo Boost up to 4.6 GHz) and 8 GB of RAM.

I measured the start and stop timestamps to calculate the total elapsed time for adding 100,000 elements to a list.

The result shows that it takes 14 seconds to perform this operation.

This seems slow (it is!). So let’s investigate some other methods to concatenate and their performance:

2. List Concatenation with append()

The list.append(x) method—as the name suggests—appends element x to the end of the list. You can read my full blog tutorial about it here.

Here’s a similar example that shows how you can use the append() method to concatenate two lists l1 and l2 and store the result in the list l1.

l1 = [1, 2, 3]
l2 = [4, 5, 6]
for element in l2:
    l1.append(element)
print(l1)

Output:

[1, 2, 3, 4, 5, 6]

It seems a bit odd to concatenate two lists by iterating over each element in one list. You’ll learn about an alternative in the next section but let’s first check the performance!

Performance:

I performed a similar experiment as before.

import time

start = time.time()

l = []
for i in range(100000):
    l.append(i)

stop = time.time()

print("Elapsed time: " + str(stop - start))

Output:

Elapsed time: 0.006505012512207031

I measured the start and stop timestamps to calculate the total elapsed time for adding 100,000 elements to a list.

The result shows that it takes only 0.006 seconds to perform this operation. This is a more than 2,000X improvement compared to the 14 seconds when using the + operator for the same problem.

While this is readable and performant, let’s investigate some other methods to concatenate lists.

3. List Concatenation with extend()

You’ve studied the append() method in the previous paragraphs. A major problem with it is that if you want to concatenate two lists, you have to iterate over all elements of the lists and append them one by one. This is complicated and there surely must be a better way. Is there?

You bet there is!

The method list.extend(iter) adds all elements in iter to the end of the list.

The difference between append() and extend() is that the former adds only one element and the latter adds a collection of elements to the list.

Here’s a similar example that shows how you can use the extend() method to concatenate two lists l1 and l2.

l1 = [1, 2, 3]
l2 = [4, 5, 6]
l1.extend(l2)
print(l1)

Output:

[1, 2, 3, 4, 5, 6]

The code shows that the extend() method is more readable and concise than iteratively calling the append() method as done before.

But is it also fast? Let’s check the performance!

Performance:

I performed a similar experiment as before for the append() method.

import time

start = time.time()

l = []
l.extend(range(100000))

stop = time.time()

print("Elapsed time: " + str(stop - start))

Output:

Elapsed time: 0.0

I measured the start and stop timestamps to calculate the total elapsed time for adding 100,000 elements to a list.

The result shows that it takes negligible time to run the code (0.0 seconds compared to 0.006 seconds for the append() operation above).

The extend() method is the most concise and fastest way to concatenate lists.

But there’s still more…

4. List Concatenation with Asterisk Operator *

There are many applications of the asterisk operator (read this blog article to learn about all of them). But one nice trick is to use it as an unpacking operator that “unpacks” the contents of a container data structure such as a list or a dictionary into another one.

Here’s a similar example that shows how you can use the asterisk operator to concatenate two lists l1 and l2.

l1 = [1, 2, 3]
l2 = [4, 5, 6]
l3 = [*l1, *l2]
print(l3)

Output:

[1, 2, 3, 4, 5, 6]

The code shows that the asterisk operator is short, concise, and readable (well, for Python pros at least). And the nice thing is that you can also use it to concatenate more than two lists (example: l4 = [*l1, *l2, *l3]).

But how about its performance?

Performance:

You’ve seen before that extend() is very fast. How does the asterisk operator compare against the extend() method? Let’s find out!

import time

l1 = list(range(0,1000000))
l2 = list(range(1000000,2000000))

start = time.time()
l1.extend(l2)
stop = time.time()

print("Elapsed time extend(): " + str(stop - start))
# Elapsed time extend(): 0.015623092651367188

start = time.time()
l3 = [*l1, *l2]
stop = time.time()

print("Elapsed time asterisk: " + str(stop - start))
# Elapsed time asterisk: 0.022125720977783203

I measured the start and stop timestamps to calculate the total elapsed time for concatenating two lists of 1,000,000 elements each.

The result shows that the extend method is 32% faster than the asterisk operator for the given experiment.

To merge two lists, use the extend() method which is more readable and faster compared to unpacking.

Okay, let’s get really nerdy—what about external libraries?

5. List Concatenation with itertools.chain()

Python’s itertools module provides many tools to manipulate iterables such as Python lists. Interestingly, they also have a way of concatenating two iterables. After converting the resulting iterable to a list, you can also reach the same result as before.

Here’s a similar example that shows how you can use the itertools.chain() method to concatenate two lists l1 and l2.

import itertools

l1 = [1, 2, 3]
l2 = [4, 5, 6]
l3 = list(itertools.chain(l1, l2))
print(l3)

Output:

[1, 2, 3, 4, 5, 6]

The result is the same. As readability is worse than using extend(), append(), or even the asterisk operator, you may wonder:

Is it any faster?

Performance:

You’ve seen before that extend() is very fast. How does the asterisk operator compare against the extend() method? Let’s find out!

import time
import itertools

l1 = list(range(2000000,3000000))
l2 = list(range(3000000,4000000))

start = time.time()
l1.extend(l2)
stop = time.time()

print("Elapsed time extend(): " + str(stop - start))
# Elapsed time extend(): 0.015620946884155273

start = time.time()
l3 = list(itertools.chain(l1, l2))
stop = time.time()

print("Elapsed time chain(): " + str(stop - start))
# Elapsed time chain(): 0.08469700813293457

I measured the start and stop timestamps to calculate the total elapsed time for concatenating two lists of 1,000,000 elements each.

The result shows that the extend() method is 5.6 times faster than the itertools.chain() method for the given experiment.

Here’s my recommendation:

Never use itertools.chain() because it’s not only less readable, it’s also slower than the extend() method or even the built-in unpacking method for more than two lists.

So let’s go back to more Pythonic solutions: everybody loves list comprehension…

6. List Concatenation with List Comprehension

List comprehension is a compact way of creating lists. The simple formula is [ expression + context ].

  • Expression: What to do with each list element?
  • Context: What list elements to select? It consists of an arbitrary number of for and if statements.

The example [x for x in range(3)] creates the list [0, 1, 2].

Need a refresher on list comprehension? Read this blog tutorial.

Here’s a similar example that shows how you can use list comprehension to concatenate two lists l1 and l2.

l1 = [1, 2, 3]
l2 = [4, 5, 6]
l3 = [ x for lst in (l1, l2) for x in lst ]
print(l3)

Output:

[1, 2, 3, 4, 5, 6]

What a mess! You’d never write code like this in practice. And yet, tutorials like this one propose this exact same method.

The idea is to first iterate over all lists you want to concatenate in the first part of the context lst in (l1, l2) and then iterate over all elements in the respective list in the second part of the context for x in lst.

Do we get a speed performance out of it?

Performance:

You’ve seen before that extend() is very fast. How does the asterisk operator compare against the extend() method? Let’s find out!

import time

l1 = list(range(4000000,5000000))
l2 = list(range(6000000,7000000))

start = time.time()
l1.extend(l2)
stop = time.time()

print("Elapsed time extend(): " + str(stop - start))
# Elapsed time extend(): 0.015620946884155273

start = time.time()
l3 = [ x for lst in (l1, l2) for x in lst ]
stop = time.time()

print("Elapsed time list comprehension: " + str(stop - start))
# Elapsed time list comprehension: 0.11590242385864258

I measured the start and stop timestamps to calculate the total elapsed time for concatenating two lists of 1,000,000 elements each.

The result shows that the extend() method is an order of magnitude faster than using list comprehension.

Summary

To concatenate two lists l1, l2, use the l1.extend(l2) method which is the fastest and the most readable.

To concatenate more than two lists, use the unpacking operator [*l1, *l2, ..., *ln].

Want to use your skills to create your dream life coding from home? Join my Python freelancer webinar and learn how to create your home-based coding business.

Click: How to Become a Python Freelancer?