all Python 3itertoolsfunction
itertools.count
itertools
An infinite counter: start, start+step, …
Syntax
itertools.count(start=0, step=1)
The counter is infinite: list(count()) or a for-loop without a break will hang and exhaust memory — bound it with islice(), zip(), or takewhile(). A common use is as an index generator paired with zip() (enumerate-style), and step may be a float.
Examples
import itertools
c = itertools.count(10)
print(next(c), next(c), next(c)) # → 10 11 12
print(list(itertools.islice(itertools.count(0, 5), 4))) # → [0, 5, 10, 15]
print(list(zip(itertools.count(1), 'abc'))) # → [(1, 'a'), (2, 'b'), (3, 'c')]
print(list(itertools.islice(itertools.count(3, -1), 5))) # → [3, 2, 1, 0, -1]
See also
Open in the full glossarydocs.python.orgReport an inaccuracy