Explain Codes LogoExplain Codes Logo

How do I make a flat list out of a list of lists?

python
functions
performance
best-practices
Anton ShumikhinbyAnton ShumikhinΒ·Sep 20, 2024
⚑TLDR

Quick and dirty list comprehension way to flatten a list of lists:

flattened = [item for sublist in outer_list for item in sublist]

Just two lines of code, and you're already flattening like a pro πŸ†!

Digging into efficiency

Let's unwrap the box 🎁 of efficient techniques, to see how well they perform.

The sleek itertools.chain

Big lists don't scare you? itertools.chain.from_iterable() might be your speed demon 🏎️:

from itertools import chain flattened = list(chain.from_iterable(outer_list))

Return of the iterator! This guy unwraps lists lazily, saving your precious memory footprint.

The lightning-fast reduce and concat

Combine functools.reduce() with operator.iconcat for electrifying speed ⚑:

from functools import reduce from operator import iconcat flattened = reduce(iconcat, outer_list, [])

Say hello to your new best friend, speed!

Measure the horsepower with perfplot

One size doesn't fit all. Use 'perfplot' to benchmark execution speed. Nothing screams efficiency better than charts πŸ“Š:

# Imagine you're a racecar driver and perfplot is your speedometer 🏁 # Do your benchmarks here

Busting the inefficiency myths

Beware of sum(outer_list, []), it holds a dark secret of quadratic complexity (O(n^2)). Can be as slow as a snail 🐌 on large datasets due to list reallocation and copying.

Other flattening contestants

As they say, the more, the merrier! Let's meet our other contenders.

numpy's try

numpy utilities for flattening lists might look tempting, but they are like a hungry Godzilla gobbling up your system resources πŸ¦–. Avoid unless numeric processing is already your playground.

The extend approach

A bit slower, but for a good reasonβ€”easy debugging and better readability:

flattened = [] for sublist in outer_list: flattened.extend(sublist)

Slow and steady does sometimes win the race 🐒.

Flattening lists with sublists

Nested lists? No problem! We have recursive flattening and itertools with unpacking on our side.

Recursive flattening

Shall we descend into the depths 🌊 of multi-level nested lists?

def flatten(lst): result = [] for item in lst: if isinstance(item, list): # Going recursive here. Don't get lost! 🧭 result.extend(flatten(item)) else: result.append(item) return result

itertools and unpacking

A sophisticated technique using itertools.chain() with the power of unpacking:

flattened = list(chain(*outer_list))

Beware, though: unpacking needs all sublists to be accessible in memory!