473,574 Members | 2,272 Online

# Counting nested loop iterations

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

Thanks,
Derek Basch

Mar 16 '06 #1
18 3697
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

what's kludgy with using a counter to count things ?

(the real question here is of course why you need the counter. what's
the loop doing? if the code you posted is all you have, you can replace
it with index = sum(len(animal) for animal in zoo), but I assume you want
to do more stuff in there...)

</F>

Mar 16 '06 #2
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)

Mar 16 '06 #3

Fredrik Lundh wrote:
what's kludgy with using a counter to count things ?
Ohhh, nothing in particular. Just seeing if there is a better way to do
it.
(the real question here is of course why you need the counter. what's
the loop doing? if the code you posted is all you have, you can replace
it with index = sum(len(animal) for animal in zoo), but I assume you want
to do more stuff in there...)

</F>

Yes, I am doing more. Your example assumes that all that animals have
the same amount of colors. Not a bad assumption considering I didn't
say anything about that. Looks like good old counters are they way to
go then.

Mar 16 '06 #4
> Depending on the types of the containers in question, you could use:

len(zoo) * len(animal)

I think this would give me the total iterations but I wouldn't be able
to get a running count. Correct?

Derek Basch

Mar 16 '06 #5
Hi Derek

I went for an embarrassingly long time without knowing about
*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

Mar 16 '06 #6
Derek Basch wrote:
Fredrik Lundh wrote:
(the real question here is of course why you need the counter. what's
the loop doing? if the code you posted is all you have, you can replace
it with index = sum(len(animal) for animal in zoo), but I assume you want
to do more stuff in there...)

</F>

Yes, I am doing more. Your example assumes that all that animals have
the same amount of colors.

No, it doesn't. Fredrik wrote
index = sum(len(animal) for animal in zoo)

The expression (len(animal) for animal in zoo) yields a sequence of the
lengths of the individual animals; passing this to sum() gives the total
count.

Kent
Mar 16 '06 #7
Derek Basch wrote:
index = 0
for animal in zoo:
for color in animal:
index += 1 # assuming there is something more goes here...

You could do this, but it kind of depends what the loop *really* looks
like:

for index, color in enumerate(color
for animal in zoo
for color in animal):
# the something more goes here.
pass
Mar 16 '06 #8
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

I don't know if it's kludgy, but I do prefer to set counters in the for
statement whenever I can. If I were using 2.4, I might try writing
something like:

for i,(animal,zoo) in enumerate((anim al,zoo) for animal in zoo for
color in animal):
pass

or maybe break it down a little for clarity:

aciter = ((animal,zoo) for animal in zoo for color in animal)
for i,(animal,zoo) in enumerate(acite r):
pass

But even the clear version isn't as nearly clear and straightforward as
the nested fors with the counter. I wouldn't forsake that clarity just
so it isn't "kludgy".
Carl Banks

Mar 16 '06 #9

Carl Banks wrote:
But even the clear version isn't as nearly clear and straightforward as
the nested fors with the counter. I wouldn't forsake that clarity just
so it isn't "kludgy".
Carl Banks

Yeah, looks like using the counters is clearer. Thanks for the opinions
everyone!

Derek Basch

Mar 16 '06 #10

This thread has been closed and replies have been disabled. Please start a new discussion.