7. Flattening data
Here, we want to flatten a list of lists into one sequence. Prefer the approach that best balances readability and performance for your case.
Flattening code often sits on hot paths or in data cleanup stages, so both readability and accidental quadratic behavior matter. A direct iterable-based approach usually communicates the shape transformation more clearly than nested append loops.
7.1. Don’t do this
1data = [list(range(10000000)) for _ in range(10)]
2
3x = []
4for arr in data:
5 for val in arr:
6 x.append(val)
1data = [list(range(10000000)) for _ in range(10)]
2
3x = []
4for arr in data:
5 x.extend(arr)
7.2. Do this
1data = [list(range(10000000)) for _ in range(10)]
2
3x = [val for arr in data for val in arr]
1import itertools
2
3data = [list(range(10000000)) for _ in range(10)]
4
5x = itertools.chain.from_iterable(data)