Counting nested loop iterations

Jeffrey Schwab jeff at schwabcenter.com
Thu Mar 16 15:24:20 EST 2006


Derek Basch wrote:
> What is the best way to count nested loop iterations? I can only figure
> to use an index but that seems kludgy.
> 
> index = 0
> for animal in zoo:
>     for color in animal:
>         index += 1

Depending on the types of the containers in question, you could use:

	len(zoo) * len(animal)






More information about the Python-list mailing list