pysnippets.lists.deep_flatten
Flatten a nested list of any depth
deep_flatten(items: Iterable[object]) -> Iterator[object]
Lazily yield every value from arbitrarily nested iterables, treating strings as values.
API responses and config files nest lists inside lists. This walks all of them and yields the values in order.
deep_flatten.py✓ ruff · mypy --strict
from collections.abc import Iterable, Iterator
def deep_flatten(items: Iterable[object]) -> Iterator[object]:
"""Yield every non-iterable value from arbitrarily nested iterables."""
for item in items:
if isinstance(item, Iterable) and not isinstance(item, str | bytes):
yield from deep_flatten(item)
else:
yield item- without this check "ab" recurses forever: "a" is iterable too
examples✓ 3 doctests · 3.11–3.14
passed on Python 3.11 to 3.14: >>> list(deep_flatten([1, [2], [[3], 4], 5]))[1, 2, 3, 4, 5]passed on Python 3.11 to 3.14: >>> list(deep_flatten(["ab", ("cd", ["ef"])]))['ab', 'cd', 'ef']passed on Python 3.11 to 3.14: >>> list(deep_flatten([[], [[]]]))[]Edit the function or the examples and run the doctests here, on Python 3.14. Python loads only when you click (about 7 MB, cached after the first time).
python 3.14 · in your browser
Loading Python 3.14 in your browser (≈7 MB, cached after the first time)…
Tab inserts four spaces. Press Escape, then Tab, to leave the editor. Control or Command plus Enter runs the examples.
How it works
yield fromrecurses into anything iterable.strandbytesare iterable, and so is each character, so without the check a string recurses until it hits the recursion limit.- It’s a generator: wrap it in
list()when you need a list.
Complexity: O(n) time, O(d) stack for depth d
- 3.11✓passes
- 3.12✓passes
- 3.13✓passes
- 3.14✓passes
Import
from pysnippets.lists.deep_flatten import deep_flattenNo dependencies: copy deep_flatten.py into your project, or clone the repo.Adapted from deep-flatten in 30-seconds-of-python by the 30 seconds of code contributors, CC BY 4.0. Changed: Rewritten as a generator; strings and bytes are treated as values. Suggest an edit.