Don't forget to also checkout my second blog containing articles to all other related ICT topics!!

Thursday, May 3, 2012

Itertools cycle method explained

The cycle method makes 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.
from itertools import cycle

start = 0
stop =  7
for number in cycle(range(0,3)):
    start += 1
    if start > stop:
        break
    print number 

0
1
2
0
1
2
0

No comments:

Post a Comment