Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tut] How to Concatenate Lists in Python? [Interactive Guide]
1
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
    Code:
    +

  2. List
    Code:
    append()
    method

  3. List
    Code:
    extend()
    method

  4. Asterisk operator
    Code:
    *

  5. Code:
    Itertools.chain()

  6. List comprehension

Code:
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:

Code:
''' 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
Code:
l1
,
Code:
l2
, use the
Code:
l1.extend(l2)
method which is the fastest and the most readable.


To concatenate more than two lists, use the unpacking (asterisk) operator
Code:
[*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.

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.

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

Output:

Code:
[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:

Code:
import time start = time.time() l = [] for i in range(100000): l = l + [i] stop = time.time() print("Elapsed time: " + str(stop - start))

Output:

Code:
Elapsed time: 14.438847541809082

The experiments were performed on my notebook with an Intel® Core™ 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
Code:
list.append(x)
method—as the name suggests—appends element
Code:
x
to the end of the
Code:
list
. You can read my full blog tutorial about it here.

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

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

Output:

Code:
[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.

Code:
import time start = time.time() l = [] for i in range(100000): l.append(i) stop = time.time() print("Elapsed time: " + str(stop - start))

Output:

Code:
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
Code:
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
Code:
list.extend(iter)
adds all elements in
Code:
iter
to the end of the
Code:
list
.

The difference between
Code:
append()
and
Code:
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
Code:
extend()
method to concatenate two lists
Code:
l1
and
Code:
l2
.

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

Output:

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

The code shows that the
Code:
extend()
method is more readable and concise than iteratively calling the
Code:
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
Code:
append()
method.

Code:
import time start = time.time() l = [] l.extend(range(100000)) stop = time.time() print("Elapsed time: " + str(stop - start))

Output:

Code:
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
Code:
append()
operation above).

The
Code:
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
Code:
l1
and
Code:
l2
.

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

Output:

Code:
[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:
Code:
l4 = [*l1, *l2, *l3]
).

But how about its performance?

Performance:

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

Code:
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 itertoolsmodule 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
Code:
itertools.chain()
method to concatenate two lists
Code:
l1
and
Code:
l2
.

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

Output:

Code:
[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
Code:
extend()
is very fast. How does the asterisk operator compare against the
Code:
extend()
method? Let’s find out!

Code:
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
Code:
extend()
method is 5.6 times faster than the
Code:
itertools.chain()
method for the given experiment.

Here’s my recommendation:

Never use
Code:
itertools.chain()
because it’s not only less readable, it’s also slower than the
Code:
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
Code:
[ 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
Code:
[x for x in range(3)]
creates the list
Code:
[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
Code:
l1
and
Code:
l2
.

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

Output:

Code:
[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
Code:
lst in (l1, l2)
and then iterate over all elements in the respective list in the second part of the context
Code:
for x in lst
.

Do we get a speed performance out of it?

Performance:

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

Code:
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
Code:
extend()
method is an order of magnitude faster than using list comprehension.

Summary

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


To concatenate more than two lists, use the unpacking operator
Code:
[*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?
Reply

Users browsing this thread:
1 Guest(s)


Forum Jump: