Flattening Nested Lists
A nested comprehension can flatten a list of lists into a single list efficiently.
A nested comprehension can flatten a list of lists into a single list efficiently.
nested = [[1,2], [3,4], [5,6]]
flat = [item for sublist in nested for item in sublist]
print(flat)
# [1, 2, 3, 4, 5, 6]
For deeply nested structures, use itertools.chain.from_iterable instead.