itertools cycle() docs question

Tobiah toby at tobiah.org
Wed Aug 21 14:27:41 EDT 2019


In the docs for itertools.cycle() there is
a bit of equivalent code given:

     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


Is that really how it works?  Why make
the copy of the elements?  This seems
to be equivalent:


     def cycle(iterable):
         while iterable:
             for thing in iterable:
                 yield thing



More information about the Python-list mailing list