itertools.count() as built-in

jantod at jantod at
Mon May 29 01:50:41 CEST 2006

I have a project of around 6000 lines where I used count() 20 times. It
has 14 modules, 10 of which I needed an explicit import.

Many of the usages are of the form:

for item, n in zip(items, count(N)):

Around half of these are due to using pylab.subplot(x,y.n), which
requires values for n>=1. So I want N=1. The enumerate builtin starts
from 0.
I also write out textual reports that start with n=1.

I also have things like
  for n, component, rotations in zip(count(),
sorted(tools.component_files().keys()), all_symbol_rotations)
where the enumerate version just looks even more confusing and easy to
mess up
  for n, component, rotations in
enumerate(sorted(tools.component_files().keys()), all_symbol_rotations)

Some more examples follow. Note that I find it more natural to specify
the count last as it's usually less important than the other values.
This is different from the order given by enumerate, so I'm more
inclined to use count().

mutated_symbols_and_names = zip(mutated_symbols, (("<%s mutation %s>" %
(component, m)) for m in count()))
edge_weight=dict(zip(Intersection.types, count(1)))
for (component, filename), n in zip(components, count()):
for (component, filename), component_count in
zip(tools.component_files().items(), count()):

More information about the Python-list mailing list