SyntaxStudy
Sign Up
Python Intermediate 3 min read

Infinite Iterators

Infinite Iterators

itertools provides count(), cycle(), and repeat() that produce infinite sequences. Always pair with islice or a break condition.

Example
from itertools import cycle, islice

colors = cycle(["red", "green", "blue"])
for color in islice(colors, 7):
    print(color)
Pro Tip

Never iterate an infinite iterator without a stopping condition.