MCPcopy Create free account
hub / github.com/dabeaz/python-cookbook / flatten

Function flatten

src/4/how_to_flatten_a_nested_sequence/example.py:5–10  ·  view source on GitHub ↗
(items, ignore_types=(str, bytes))

Source from the content-addressed store, hash-verified

3from collections import Iterable
4
5def flatten(items, ignore_types=(str, bytes)):
6 for x in items:
7 if isinstance(x, Iterable) and not isinstance(x, ignore_types):
8 yield from flatten(x)
9 else:
10 yield x
11
12items = [1, 2, [3, 4, [5, 6], 7], 8]
13

Callers 1

example.pyFile · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected