Best way to have a for-loop index?

Roy Smith roy at panix.com
Thu Mar 9 20:22:34 EST 2006


In article <1141950744.482666.4430 at j33g2000cwa.googlegroups.com>,
 andrewfelch at gmail.com wrote:

> I write a lot of code that looks like this:
> 
> for myElement, elementIndex in zip( elementList,
> range(len(elementList))):
>     print "myElement ", myElement, " at index: ",elementIndex
> 
> 
> My question is, is there a better, cleaner, or easier way to get at the
> element in a list AND the index of a loop than this?
> 
> TIA,
> Andrew

The real question is *why* do you want the index?

If you're trying to iterate through list indicies, you're probably trying 
to write C, C++, Fortran, Java, etc in Python.

Can you describe exactly what it is that you're trying to do?



More information about the Python-list mailing list