can I get the index number in for x in y loop?

Fuzzyman fuzzyman at gmail.com
Mon Apr 3 17:27:14 EDT 2006


JuHui wrote:
> >>> a='String'
> >>> for x in a:
> ...     print x
> ...
> S
> t
> r
> i
> n
> g
> >>>
>
> can I get the index number  of a in the upon loop within for x in a
> loop?

Although enumerate is the 'right' answer, I personally prefer :


i = 0
while i < len(some_sequence):
    val = some_sequence[i]
    ...
    i += 1

This is so that I can manually wind 'i' backwards and forwards manually
from other parts of the code.

Fuzzyman
http://www.voidspace.org.uk/python/index.shtml




More information about the Python-list mailing list