Python Glossary Русский

all Python 3itertoolsfunction

itertools.tee

itertools

Create n independent iterators from a single iterable.

Syntax

itertools.tee(iterable, n=2)

After tee, stop using the original iterator and work only through the returned branches. If one branch races far ahead of another, tee buffers every element in between in memory — so for a large gap a plain list() is cheaper — and the branches are not thread-safe.

Examples

import itertools
a, b = itertools.tee([1, 2, 3])
print(list(a), list(b))   # → [1, 2, 3] [1, 2, 3]
print(list(a))   # → []
c, d, e = itertools.tee([1, 2, 3], 3)
print(next(c), next(c), next(d), list(e))   # → 1 2 1 [1, 2, 3]

See also

Open in the full glossarydocs.python.orgReport an inaccuracy