ID EN
Itertools

cycle

Python 3.11

Make an iterator returning elements from the iterable and saving a copy of each. When the iterable is exhausted, return elements from the saved copy. Repeats indefinitely. Roughly equivalent to:

Syntax

PYTHON
itertools.cycle(iterable)

Examples

Example 1
PYTHON
def cycle(iterable):
     cycle('ABCD') --> A B C D A B C D A B C D ...
    saved = []
    for element in iterable:
        yield element
        saved.append(element)
    while saved:
        for element in saved:
              yield element