Counting nested loop iterations

Caleb Hattingh caleb.hattingh at gmail.com
Thu Mar 16 15:57:18 EST 2006


Hi Derek

I went for an embarrassingly long time without knowing about
"enumerate()".  It doesn't directly answer your question about counting
*within* nests, but I am going to tell you this on the off chance you
don't know yet (and apologies if you do):

This:

count = 0
for animal in zoo:
    a = animal
    anum = count
    count = count + 1

IS a kludge, when you have this available to you:

for count, animal in enumerate(zoo):
    a = animal
    anum = count

I won't say how long it took me to start using list comprehensions :)

regards
Caleb




More information about the Python-list mailing list