itertools.tee(iterable, n=2) splits one iterable into n independent iterators that each yield the same items. It lets you walk through a single-pass source, such as a generator or a file, more than once without reading it twice, because tee stores the items that one iterator has consumed and another has not.
The iterators it returns are meant for use from one thread, so add your own locking if several threads share them.
itertools.tee() doesitertools.tee() doesA generator can be consumed only once. After you loop over it, it is empty. tee solves this by returning several iterators that share an internal buffer.
Without tee, the second list(...) would print []. The signature is tee(iterable, n=2), and it always returns a tuple of n iterators.
tee does not copy the whole input up front. When one iterator asks for an item the others have not seen yet, tee pulls it from the source and keeps it in a buffer until every iterator has passed it. Memory use therefore depends on how far apart the iterators drift.
After advancing a by 1,000 items, the buffer holds those 1,000 values until b catches up. If you consume one iterator completely before touching the others, the whole sequence ends up in memory, and a plain list(iterable) is simpler and faster.
A classic use of tee is looking at each item together with its successor. Both iterators stay one step apart, so the buffer stays tiny.
Since Python 3.10 the standard library offers itertools.pairwise(), which does exactly this, so use it in new code. Python 3.12 also added itertools.batched() for fixed-size chunks.
You can run two independent calculations over the same stream in one pass over the source.
Here the first iterator is exhausted before the second starts, so the buffer holds all five values. This is fine for five items but wasteful for millions. When the data is small, store it in a list. When it is huge, restructure the code to compute both results in a single loop.
tee. Advancing it directly makes the new iterators skip items.copy.deepcopy, tee does not copy the items. Mutable objects yielded by the source are shared by every iterator.tee: iterate the sequence again.| Situation | Better choice |
|---|---|
| Data fits in memory and is reused | list(iterable) |
| Items and their successors | itertools.pairwise() |
| Fixed-size chunks | itertools.batched() (3.12+) |
| Large stream, iterators stay close together | itertools.tee() |
tee a function or a method?It is a function in the itertools module, so you call it as itertools.tee(iterable) or import it with from itertools import tee. There is no built-in tee function.
tee copy the data?No. It buffers items lazily and only for as long as some iterator still needs them. The items themselves are not copied.
Any number, with tee(iterable, n). Each additional iterator costs a little more bookkeeping, but the buffer is still shared.
tee related to the Unix tee command?Only by name and idea. The shell command copies its input to a file and to standard output, while itertools.tee duplicates a Python iterator.
itertools.tee turns one single-pass iterable into several independent iterators that share a buffer. It works well when the iterators advance at similar speeds, as in pairwise comparisons, and poorly when one is fully consumed first. Prefer list() for small reusable data, itertools.pairwise() for neighbours, and never touch the source iterable after calling tee.