ID EN
Itertools

tee

Python 3.11

Return n independent iterators from a single iterable.

Syntax

PYTHON
itertools.tee(iterable, n=2)

Examples

Example 1
PYTHON
def tee(iterable, n=2):
    it = iter(iterable)
    deques = [collections.deque() for i in range(n)]
    def gen(mydeque):
        while True:
            if not mydeque:              when the local deque is empty
                try:
                    newval = next(it)    fetch a new value and
                except StopIteration:
                    return
                for d in deques:         load it to all the deques
                    d.append(newval)
            yield mydeque.popleft()
    return tuple(gen(d) for d in deques)

See Also

tee() RuntimeError tee() list() tee()