osdir.com


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

itertools cycle() docs question


The point is to demonstrate the effect, not the specific implementation.

On Wed, Aug 21, 2019 at 2:30 PM Tobiah <toby at tobiah.org> wrote:

> 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
> --
> https://mail.python.org/mailman/listinfo/python-list
>


-- 

CALVIN SPEALMAN

SENIOR QUALITY ENGINEER

cspealma at redhat.com  M: +1.336.210.5107
[image: https://red.ht/sig] <https://red.ht/sig>
TRIED. TESTED. TRUSTED. <https://redhat.com/trusted>