On this page
itertools.count
Prev: itertools.compress | Up: itertools | Next: itertools.cycle
Rel: itertools.accumulate, itertools.batched, itertools.chain, itertools.combinations
Ref: docs.python.org — itertools.count
Tags: #public #python3 #stdlib #itertools
Import
import itertools
from itertools import count
Signature & Overview
itertools.count(start=0, step=1)
Return a count object whose .next() method returns consecutive values.
Usage
from itertools import count, islice
assert list(islice(count(start=10, step=5), 4)) == [10, 15, 20, 25]
# Handy with zip() to attach custom starting indices
assert list(zip(count(1), ["a", "b"])) == [(1, "a"), (2, "b")]
Gotchas
- Infinite iterator: Never call
list(count())or iterate without a termination condition (islice,zip,takewhile, orbreak). - Float step drift:
count(0.0, 0.1)accumulates floating-point rounding error over many iterations; preferDecimalor integer scaling for exact decimals.
REPL Inspect
from itertools import count
help(count)
Linked from
Related notes
- tuts/python3/stdlib_module/itertools/compress Linked here; links to this page; shared tags: #itertools, #stdlib
- tuts/python3/stdlib_module/itertools/cycle Linked here; links to this page; shared tags: #itertools, #stdlib
- tuts/python3/stdlib_module/itertools Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/itertools/accumulate Linked here; shared tags: #itertools, #stdlib
- tuts/python3/stdlib_module/itertools/batched Linked here; shared tags: #itertools, #stdlib
- tuts/python3/stdlib_module/itertools/chain Linked here; shared tags: #itertools, #stdlib
- tuts/python3/stdlib_module/itertools/combinations Linked here; shared tags: #itertools, #stdlib
- tuts/python3/stdlib_module/itertools/combinations_with_replacement shared tags: #itertools, #stdlib
- tuts/python3/stdlib_module/itertools/dropwhile shared tags: #itertools, #stdlib
- tuts/python3/stdlib_module/itertools/filterfalse shared tags: #itertools, #stdlib