PyPython example

How to flatten a list of lists in Python

4 min read▶ Runs in an isolated hosted runtimeUpdated Jul 2026

Quick answer

The most Pythonic way to flatten one level is a nested comprehension: [x for sublist in nested for x in sublist]. It reads the two for clauses left to right — outer list first, then each item — and returns a new flat list.

"Flattening" turns a list of lists like [[1, 2], [3, 4]] into a single flat list [1, 2, 3, 4]. The methods below all flatten one level — perfect for a plain list of lists — and the last section handles lists nested to any depth. Each example runs on this page: hit Run, then edit the code and run it again.

1Nested list comprehensionRecommended

A comprehension with two for clauses walks each sublist, then each item inside it. Read it left to right: "for every sublist in nested, for every x in that sublist." It's the idiomatic, fast, no-imports choice.

flatten.py

Output

The clause order matches a nested for loop: the outer loop comes first. If you flip them you'll get a NameError — the inner name has to be introduced before it's used.

2itertools.chain.from_iterable()

chain.from_iterable() lazily strings the sublists together into one stream, which list() then materializes. It never builds intermediate lists, so it's memory-efficient and reads as exactly what it does — chain these iterables end to end.

flatten.py

Output

Because it's lazy, you can iterate the result without list() when you only need to loop once — handy for very large or streamed data.

3sum(nested, []) (short, but slow)

sum() can add lists too: start from an empty list and + each sublist onto it. It's the shortest one-liner — but every + copies the whole result so far, making it O(n²). Cute for tiny lists, a real trap on big ones.

flatten.py

Output

The [] second argument is the required start value. Skip it and sum() defaults to 0, which raises TypeError: unsupported operand type(s) for +: 'int' and 'list'. Prefer the comprehension or chain for anything but toy input.

4Which should you use?

MethodSpeedReadabilityBest for
[x for sub in nested for x in sub]FastGreatMost cases
chain.from_iterable()FastGreatLarge / lazy data
sum(nested, [])Slow (O(n²))ShortestTiny lists only

5Common variation: flatten arbitrarily nested lists

The three methods above only remove one level of nesting. When lists are nested to unknown depth — [1, [2, [3, 4]], 5] — recurse: walk each item, flatten it if it's a list, otherwise keep it:

deep_flatten.py

Output

The isinstance(item, list) check is what makes it depth-agnostic — each nested list is expanded by the same function. Swap in (list, tuple) to flatten mixed sequences too.

Frequently asked questions

What is the most Pythonic way to flatten a list of lists?

A nested list comprehension: [x for sublist in nested for x in sublist]. It is fast, needs no imports, and reads left to right — outer loop first, then each item. itertools.chain.from_iterable(nested) is an equally idiomatic alternative that is lazy and memory-efficient.

Why is sum(nested, []) slow?

Each + builds a brand-new list by copying everything accumulated so far, so flattening n sublists does roughly n² copies — O(n²). It is fine for a handful of items but degrades badly on large inputs. Use the comprehension or chain.from_iterable() instead.

How do I flatten a list nested to arbitrary depth?

Recurse: for each item, flatten it if it is itself a list, otherwise append it. See the flatten() function in the variation above. The one-level methods (comprehension, chain, sum) only remove a single layer of nesting.

How do I flatten a NumPy array or nested arrays?

NumPy has its own tools: arr.flatten() returns a flattened copy and arr.ravel() returns a flattened view when possible (faster, no copy). Those are for rectangular n-dimensional arrays — for a ragged Python list of lists, stick with the comprehension or chain.from_iterable().