Concatenate lists in Python
Choose list addition, unpacking, extend, or itertools.chain based on whether you need a new list, in-place changes, or lazy iteration.
Use a + b or [*a, *b] to create a new list. Use a.extend(b) to change an existing list, or itertools.chain(a, b) to iterate over both without first building a combined list.
Create a new list
a = [1, 2, 3]
b = [4, 5, 6]
combined = a + b
unpacked = [*a, *b]
print(combined) # [1, 2, 3, 4, 5, 6]
print(a) # [1, 2, 3]
List addition requires lists on both sides. Unpacking also accepts other iterables. Both create a new outer list, but they do not deep-copy its elements: nested mutable objects remain shared.
Extend a list in place
a = [1, 2, 3]
b = [4, 5, 6]
a.extend(b)
print(a) # [1, 2, 3, 4, 5, 6]
extend() returns None, so do not write a = a.extend(b). Every reference to the original list sees the added elements. The list still needs storage for the additions; in-place does not mean no memory allocation.
Iterate without materializing the combination
from itertools import chain
a = [1, 2, 3]
b = [4, 5, 6]
for value in chain(a, b):
print(value)
For many input lists, use chain.from_iterable(lists). It produces an iterator; wrapping it in list() allocates the combined list after all.
| Requirement | Suitable option |
|---|---|
| Keep the inputs and obtain a new list | a + b or [*a, *b] |
| Add to an existing list | a.extend(b) |
| Consume many inputs lazily | chain.from_iterable(lists) |
| Transform or filter while combining | A nested list comprehension or generator |
For example, [value for group in lists for value in group] flattens one level. Repeatedly using + in a growing loop copies intermediate lists; extend() or chain usually expresses that job better.
FAQ
How is append() different? a.append(b) adds b as one element, creating a nested list if b is a list. extend() adds each element from the iterable.
Does + change either input? No. It creates a new list. The examples above reset the inputs between methods so the in-place example does not accidentally duplicate later results.
References
Contact us
Join the community
to join the community
Try Guance
Start online and pay only for what you use.
Get startedChoose a Guance plan