Explain Codes LogoExplain Codes Logo

How do I concatenate two lists in Python?

python
functions
collections
best-practices
Anton ShumikhinbyAnton Shumikhin·Sep 13, 2024
TLDR

Just add them together using the + operator:

# A quick list addition - the ol' 1, 2, 3 and 4, 5, 6 dance combined = [1, 2, 3] + [4, 5, 6] print(combined) # Prints: [1, 2, 3, 4, 5, 6]

It's as simple as that. This method maintains the order and contents and leaves the originals untouched. Great for those "get-in, get-out" moments in coding.

Multi-solution sprint

Now let's dive into some of the other concatenation methods and discuss their use-cases:

Unpacking operator

The * unpacking operator, an avantgarde choice since Python 3.5:

# More stars here than in a cheap sci-fi movie combined = [*list_one, *list_two]

Extend for the win

Want to grow your list without making more? Use the extend() method:

# This operation is so 'extend'ed, it might need a vacation list_one.extend(list_two)

This modifies list_one without the need for a new list.

itertools.chain - smart and efficient

When dealing with large or multiple lists, trust itertools.chain() to handle it smoothly:

import itertools # chain's playing, but this ain't no weak link combined = list(itertools.chain(list_one, list_two))

Or, for lists containing lists:

combined = list(itertools.chain.from_iterable(list_of_lists)) # It's an Inception of lists

No intermediate lists ensure we're being memory efficient.

heapq.merge: a sorted affair

When your sorted lists need friendly merging, heapq.merge is the perfect cup of tea:

import heapq # It's high time we 'heap'ed them together! combined_sorted = list(heapq.merge(sorted_list_one, sorted_list_two))

This keeps your combined array sorted.

Manual for loop

If you'd like more control over concatenation:

# 'for' those who enjoy the scenic route combined = [] for list in [list_one, list_two]: combined += list # a smaller 'add'ition to the family

Size matters: handling large lists

When dealing with big lists, the + operator can hog memory. Here, itertools.chain() proves to be a memory-efficient hero.

Pitfall prevention

To be an effective Pythonista, it's essential to understand common pitfalls and avoid them. Let's discuss some:

Not all sums are equal

Be wary of the sum() function, it often leads to high-runtime complexity:

# Seems like the 'sum' of all inefficiencies combined = sum(list_of_lists, [])

For functional folks

For fans of the functional paradigm, operator.add is the equivalent of the + operator:

import operator # When regular addition feels too mainstream combined = operator.add(list_one, list_two)

Wacky concatenation: list comprehensions

For a creative approach, enter list comprehensions:

# Because we can, that's why combined = [item for pair in zip(list_one, list_two) for item in pair]

Need originals? Meet their clones

If you need to concatenate but keep originals, hello new variable:

combined = list_one + list_two # list_one and list_two remain unchanged