Search code examples
pythoncyclepython-itertools

In Python, why does itertools.cycle require additional memory?


I'm curious about the warning in itertools.cycle(iterable) :

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.

Equivalent to:

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

The entry also contains the warning, "Note, this member of the toolkit may require significant auxiliary storage (depending on the length of the iterable)."

Couldn't you avoid the additional storage requirement (and some complexity) with:

def cycle(iterable):
    while True:
        for i in iterable:
            yield i

What's the advantage of storing used items in saved?


Solution

  • Some iterables can only be iterated once. Thus cycle will store a copy so it can continue to read those items. See this related question.