By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,276 Members | 1,452 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,276 IT Pros & Developers. It's quick & easy.

enumerate() question

P: n/a
Hello!
I have a question for the developer[s] of enumerate(). Consider the
following code:

for x,y in coords(dots):
print x, y

When I want to iterate over enumerated sequence I expect this to work:

for i,x,y in enumerate(coords(dots)):
print i, x, y

Unfortunately, it doesn't =( and I should use (IMHO) ugly

for i,pair in enumerate(coords(dots)):
print i, pair[0], pair[1]

So, why enumerate() works this way and is there any chance of changing
the behaviour?

--
Regards, Gregory.

May 22 '06 #1
Share this Question
Share on Google+
6 Replies


P: n/a
Gregory Petrosyan a écrit :
Hello!
I have a question for the developer[s] of enumerate(). Consider the
following code:

for x,y in coords(dots):
print x, y

When I want to iterate over enumerated sequence I expect this to work:

for i,x,y in enumerate(coords(dots)):
print i, x, y

Unfortunately, it doesn't =( and I should use (IMHO) ugly
Try:

for i,(x,y) in enumerate(coords(dots)) :
...

for i,pair in enumerate(coords(dots)):
print i, pair[0], pair[1]

So, why enumerate() works this way and is there any chance of changing
the behaviour?


No chance, behavior is correct.

A+

L.Pointal.
May 22 '06 #2

P: n/a
Gregory Petrosyan wrote:
Hello!
I have a question for the developer[s] of enumerate(). Consider the
following code:

for x,y in coords(dots):
print x, y

When I want to iterate over enumerated sequence I expect this to work:

for i,x,y in enumerate(coords(dots)):
print i, x, y

Unfortunately, it doesn't =( and I should use (IMHO) ugly

for i,pair in enumerate(coords(dots)):
print i, pair[0], pair[1]
Use:

for i, (x, y) in enumerate(coords(dots)):
print i, x, y
So, why enumerate() works this way and is there any chance of changing
the behaviour?


Because enumerate has no way to distinguish between iterables you do and
don't want unpacked. So, for example, this wouldn't work under your
proposal:

for index, string in ["foo", "bar", "baz"]:
print "String number %s is %s." % (index, string)

But this would:

for index, x, y, z in ["foo", "bar", "baz"]:
print "First character of string number %s is %s." % (index, x)
May 22 '06 #3

P: n/a
> I have a question for the developer[s] of enumerate(). Consider the
following code:

for x,y in coords(dots):
print x, y

When I want to iterate over enumerated sequence I expect this to work:

for i,x,y in enumerate(coords(dots)):
print i, x, y


for i, (x, y) in enumerate(coords(dots)):
print i, x, y

--
damjan
May 22 '06 #4

P: n/a
Gregory Petrosyan wrote:
Hello!
I have a question for the developer[s] of enumerate(). Consider the
following code:

for x,y in coords(dots):
print x, y

When I want to iterate over enumerated sequence I expect this to work:

for i,x,y in enumerate(coords(dots)):
print i, x, y

Unfortunately, it doesn't =( and I should use (IMHO) ugly

for i,pair in enumerate(coords(dots)):
print i, pair[0], pair[1]

So, why enumerate() works this way and is there any chance of changing
the behaviour?


It works that way because enumerate returns a tuple - index, value. And it
doesn't care what is inside value. Actually, it can't - how would you then
write something like this?

l = [1, ('a', 'tuple'), 3]

for i, value in enumerate(l):
print i, value
But your problem can be solved in an elegant fashion anyway. When *you* know
the structure of the values (and who else does?), you can simply use nested
sequence unpacking:

for i, (x, y) in enumerate(coords):
pass

HTH,

Diez

May 22 '06 #5

P: n/a
Thanks a lot.

May 22 '06 #6

P: n/a
Gregory Petrosyan wrote:
Hello!
I have a question for the developer[s] of enumerate(). Consider the
following code:

for x,y in coords(dots):
print x, y

When I want to iterate over enumerated sequence I expect this to work:

for i,x,y in enumerate(coords(dots)):
print i, x, y

Unfortunately, it doesn't =( and I should use (IMHO) ugly

for i,pair in enumerate(coords(dots)):
print i, pair[0], pair[1]

So, why enumerate() works this way
Special cases aren't special enough to break the rules.

enumerate shouldn't behave differently for different types of items.
and is there any chance of changing
the behaviour?


No, not really.

Instead, try this:

for i, (x, y) in enumerate(coords(dots)):
print i, x, y

--
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless enigma
that is made terrible by our own mad attempt to interpret it as though it had
an underlying truth."
-- Umberto Eco

May 22 '06 #7

This discussion thread is closed

Replies have been disabled for this discussion.