enumerate() question

Robert Kern robert.kern at gmail.com
Mon May 22 16:42:12 CEST 2006


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




More information about the Python-list mailing list