site logo

Ask. Code. Learn. Grow with the Developer Community.


Category: (All)
❮  Go Back

How to Flatten a List of Lists in Python Efficiently

When working with Python, it’s common to encounter lists of lists — nested collections where each inner list contains a sequence of elements.

For example:


[
[1, 2, 3],
[4, 5, 6],
[7],
[8, 9]
]


If you want to merge or flatten this structure into a single list like [1, 2, 3, 4, 5, 6, 7, 8, 9], Python offers several approaches.

Some are much faster and more memory-efficient than others, depending on how deeply nested your lists are.

How to Flatten a List of Lists in Python Efficiently

coldshadow44 on 2025-10-15



1






Showing comments related to this post.

_coldshadow44

2025-10-15

1. Fastest and Most Pythonic Solution – List Comprehension

A nested list comprehension is the most efficient and readable way to flatten a single-level list of lists:

xss = [
[1, 2, 3],
[4, 5, 6],
[7],
[8, 9]
]

flat_list = [x for xs in xss for x in xs]
print(flat_list)
# Output: [1, 2, 3, 4, 5, 6, 7, 8, 9]


How it works:

  1. The outer loop (for xs in xss) iterates through each inner list.
  2. The inner loop (for x in xs) extracts each item and adds it to the result list.

This method is both concise and high-performance.


2. Equivalent Code Using Loops

For beginners, the same logic can be written using regular for-loops:


flat_list = []
for xs in xss:
for x in xs:
flat_list.append(x)

Both methods produce the same output, but the list comprehension is more idiomatic and performs slightly better in most cases.


3. Wrap It in a Function

To reuse this logic throughout your codebase, you can define a helper function:


def flatten(xss):
return [x for xs in xss for x in xs]

Example:


numbers = [[1, 2], [3, 4, 5], [6]]
print(flatten(numbers))
# Output: [1, 2, 3, 4, 5, 6]

4. Performance Comparison

Using Python’s built-in timeit module, you can measure how fast different methods run:


$ python -mtimeit -s'xss=[[1,2,3],[4,5,6],[7],[8,9]]*99' '[x for xs in xss for x in xs]'
10000 loops, best of 3: 143 usec per loop


Compare that to slower methods:

$ python -mtimeit -s'xss=[[1,2,3],[4,5,6],[7],[8,9]]*99' 'sum(xss, [])'
1000 loops, best of 3: 969 usec per loop

$ python -mtimeit -s'xss=[[1,2,3],[4,5,6],[7],[8,9]]*99' 'reduce(lambda xs, ys: xs + ys, xss)'
1000 loops, best of 3: 1.1 msec per loop


Result:

List comprehensions are about 7x faster than using sum() or reduce() because they create the list only once, instead of repeatedly copying partial results.


5. Why Not Use sum() or + to Flatten Lists

Although the following works:


flat_list = sum(xss, [])

…it’s inefficient.

Each concatenation with + creates a new list, copying all existing elements every time.

This results in O(L²) time complexity, where L is the number of sublists — very costly for large datasets.


Summary:

  1. Best Method: [x for xs in xss for x in xs] — Fast, Pythonic, and simple.
  2. ⚙️ Alternative: Nested for-loops for clarity.
  3. Avoid: Using sum() or reduce() due to poor performance.
  4. 🧩 Tip: For deeply nested lists, consider recursive flattening methods (see: Flatten an irregular nested list in Python).

In short:

Use a list comprehension to flatten your list of lists efficiently and cleanly in Python.



Member's Sites: