Yes, enumerate is useful for that. But even if you did not now about it you could have used count from itertools to get the same effect:
import itertools
for (index, item) in zip(itertools.count(0), "hallo"):
print index, item
Itertools is worth checking out for other goodies, too.
Re:Hidden features of Python
by infinite8s on Thursday July 15, 2010 @05:39PM
(#1519311)
Before enumerate, the standard idiom for this was:
for i in range(len(a)):
print i, a[i]Seriously? You must enjoy your O(n^2).
Re:Hidden features of Python
by RyanMcGreal on Wednesday July 14, 2010 @10:28AM
(#1514635)
The sets I'm working on tend not to tend toward infinity.
Now that's a feature I wish I knew about sooner. I've been using:
which has always felt kludgy.