QuickSnip
Browse
Tags
+ New
← Back to snippet
Edit snippet
Title
Description
(optional)
Python one-liner to flatten a list of lists.
Language
JavaScript
TypeScript
JSX
TSX
Python
Go
Rust
Bash
SQL
CSS
HTML
JSON
YAML
Markdown
Tags
Loading tags…
Code
def flatten(nested): """Flatten an arbitrarily nested list of lists.""" return [ item for element in nested for item in (flatten(element) if isinstance(element, list) else [element]) ] print(flatten([1, [2, [3, 4]], 5])) # [1, 2, 3, 4, 5]
Update snippet
Cancel