How to Concatenate Lists in Python

John John (304)

In Python, we often run into scenarios where we need to merge two or more lists. This is also called list concatenation, and Python provides multiple methods for accomplishing this. In this guide, I'll cover a few methods, including a method for merging large lists when memory usage is a concern.

Posted in these interests:
h/python67 guides
h/code69 guides

Python allows us to easily concatenate two or more lists using the + operator. See the following example:

list_one = [0, 1, 2]
list_two = [3, 4, 5]

new_list = list_one + list_two


>>> new_list
[0, 1, 2, 3, 4, 5]

It should be noted that neither list_one nor list_two are affected by this operation. Rather the contents of each list are joined together to form a new list.

When you're dealing with potentially large lists and you need to be conscious of memory usage, you might consider using a Python generator instead. The + operator will concatenates lists, but it will create a new list in memory. If the goal is to process elements from the joined list, we can achieve this with a generator.

Suppose we have two giant lists: giant_list_one and giant_list_two. I won't initialize the lists in the example, but assume they take up a lot of memory.

import itertools

for item in itertools.chain(giant_list_one, giant_list_two):
    # operate on each item

The itertools library is handy for memory efficient looping. itertools.chain is a generator function that returns each element of the first list until it is exhausted, then returns each element from the next list, and the method can actually take any number of iterables.

Sometimes we want to merge lists, but return only unique values. If this is the case, we probably don't care about order, and we can use sets to deduplicate items.

fruit = ['apple', 'banana', 'orange']
more_fruit = ['peach', 'apple', 'banana']

unique_fruit = list(set(fruit + more_fruit))


>>> unique_fruit
['banana', 'orange', 'apple', 'peach']

Notice, how we've discarded duplicate items from the list. If you can deconstruct the example, what we've done is simply:

  • Create a new list with all elements from both lists (using the + operator).
  • Create a set from the new list, which will automatically remove duplicate items (by definition).
  • Create a final list from our set.

With the acceptance of PEP 448, implemented in Python 3.5+, you can also use the iterable unpacking operator: *. See the following example:

list_one = [0, 1, 2]
list_two = [3, 4, 5]

new_list = [*list_one, *list_two]


>>> new_list
[0, 1, 2, 3, 4, 5]

One of the benefits of using this method is that you can merge any iterables, not just lists.

new_list = [*some_list, *some_tuple, *some_generator]

In my opinion, this is much more readable than using the + operator:

new_list = some_list + list(some_tuple) + list(some_generator)
John John (304)
5 minutes

In this guide we'll cover how to merge two (or more) dictionaries in Python. There are a few ways to do it, so we'll cover multiple methods.