On this page
  1. itertools.chain
  2. Import
  3. Signature & Overview
  4. Methods & Attributes
  5. Usage
  6. Gotchas
  7. REPL Inspect

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

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

REPL Inspect

from itertools import chain
help(chain)

Linked from

Related notes