object's list index

William Meyer wmmeyer at gmail.com
Fri Mar 3 13:40:47 CET 2006


Iain King <iainking <at> gmail.com> writes:

>  what's wrong with:
> 
> i = 0
> for object in list:
>     objectIndex = i
>     print objectIndex
>     i += 1
> 
> Iain
> 

The issues with that is you might have a complex structure below the for object
in list: with lots of continues or breaks and you don't want to have to remember
to change the index everytime. There is an old pep
(http://www.python.org/peps/pep-0212.html) that describes some proposed
solutions, but I was wondering if anything has happened since aug 2000. I might
just use the 

for objectIndex in range(len(list)):
    e = list[objectIndex]

solution, though its ugly too.





More information about the Python-list mailing list