Infinite Iterators
itertools provides count(), cycle(), and repeat() that produce infinite sequences. Always pair with islice or a break condition.
itertools provides count(), cycle(), and repeat() that produce infinite sequences. Always pair with islice or a break condition.
from itertools import cycle, islice
colors = cycle(["red", "green", "blue"])
for color in islice(colors, 7):
print(color)
Never iterate an infinite iterator without a stopping condition.