Cycle through elements in an iterator

suggest change

cycle is an infinite iterator.

>>> import itertools as it
>>> it.cycle('ABCD')
A B C D A B C D A B C D ...

Therefore, take care to give boundaries when using this to avoid an infinite loop. Example:

>>> # Iterate over each element in cycle for a fixed range
>>> cycle_iterator = it.cycle('abc123')
>>> [next(cycle_iterator) for i in range(0, 10)]
['a', 'b', 'c', '1', '2', '3', 'a', 'b', 'c', '1']

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents