Looking at the next element in a for loop

Tor Iver Wilhelmsen tor.iver.wilhelmsen at broadpark.no
Sun May 2 06:55:57 EDT 2004


madsurfer2000 at hotmail.com (-) writes:

> Is there a way I can get the next element in the loop? Something like this:
> 
> for a in b:
>    if a == 1:
>       print <next a>

No, not as long as you use an iterator. If you use an index instead,
it's easy of course.

for i in xrange(len(b)):
    if b[i] is 1:
        print b[i+1]



More information about the Python-list mailing list