On this page
itertools.chain
Prev: itertools.batched | Up: itertools | Next: itertools.combinations
Rel: itertools.accumulate, itertools.batched, itertools.combinations, itertools.combinations_with_replacement
Ref: docs.python.org — itertools.chain
Tags: #public #python3 #stdlib #itertools
Import
import itertools
from itertools import chain
Signature & Overview
itertools.chain(*iterables)
Return a chain object whose .next() method returns elements from the first iterable until it is exhausted, then elements from the next iterable, until all of the iterables are exhausted.
Methods & Attributes
chain.from_iterable(iterable, /)— Alternative chain() constructor taking a single iterable argument that evaluates lazily.
Usage
from itertools import chain
assert list(chain([1, 2], (3, 4), [5])) == [1, 2, 3, 4, 5]
# Flatten one level of an iterable of iterables lazily
nested = [[1, 2], [3], [4, 5]]
assert list(chain.from_iterable(nested)) == [1, 2, 3, 4, 5]
Gotchas
- Prefer
chain.from_iterable(nested)overchain(*nested): Star-unpacking materializes the entire outer sequence into memory first, whereasfrom_iterable()stays lazy. - Flattens only one level: Strings inside a list will be iterated character-by-character if passed directly to
chain.from_iterable(["ab", "cd"]).
REPL Inspect
from itertools import chain
help(chain)
Linked from
- tuts/python3/stdlib_module/itertools
- tuts/python3/stdlib_module/itertools/accumulate
- tuts/python3/stdlib_module/itertools/batched
- tuts/python3/stdlib_module/itertools/combinations
- tuts/python3/stdlib_module/itertools/combinations_with_replacement
- tuts/python3/stdlib_module/itertools/compress
- tuts/python3/stdlib_module/itertools/count
- tuts/python3/stdlib_module/itertools/cycle
- tuts/python3/stdlib_module/itertools/dropwhile
- tuts/python3/stdlib_module/itertools/filterfalse
- tuts/python3/stdlib_module/itertools/groupby
- tuts/python3/stdlib_module/itertools/islice
- tuts/python3/stdlib_module/itertools/pairwise
- tuts/python3/stdlib_module/itertools/permutations
- tuts/python3/stdlib_module/itertools/product
- tuts/python3/stdlib_module/itertools/repeat
- tuts/python3/stdlib_module/itertools/starmap
- tuts/python3/stdlib_module/itertools/takewhile
- tuts/python3/stdlib_module/itertools/tee
- tuts/python3/stdlib_module/itertools/zip_longest
Related notes
- tuts/python3/stdlib_module/itertools/accumulate Linked here; links to this page; shared tags: #itertools, #stdlib
- tuts/python3/stdlib_module/itertools/batched Linked here; links to this page; shared tags: #itertools, #stdlib
- tuts/python3/stdlib_module/itertools/combinations Linked here; links to this page; shared tags: #itertools, #stdlib
- tuts/python3/stdlib_module/itertools/combinations_with_replacement Linked here; links to this page; shared tags: #itertools, #stdlib
- tuts/python3/stdlib_module/itertools Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/itertools/compress links to this page; shared tags: #itertools, #stdlib
- tuts/python3/stdlib_module/itertools/count links to this page; shared tags: #itertools, #stdlib
- tuts/python3/stdlib_module/itertools/cycle links to this page; shared tags: #itertools, #stdlib
- tuts/python3/stdlib_module/itertools/dropwhile links to this page; shared tags: #itertools, #stdlib
- tuts/python3/stdlib_module/itertools/filterfalse links to this page; shared tags: #itertools, #stdlib