Python One-Liner to Flatten a Nested List

By: Paypal August 4, 2025 Python

Description

Flatten a deeply nested list using a Python one-liner with list comprehension

Code Snippet

nested = [[1, 2], [3, 4], [5]]
flat = [item for sublist in nested for item in sublist]
print(flat)  # [1, 2, 3, 4, 5]

Discussion (0)