Looking at the next element in a for loop
Batista, Facundo
FBatista at uniFON.com.ar
Mon May 3 08:26:17 EDT 2004
[madsurfer2000 at hotmail.com]
#- 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>
>>> lista = ['1', 'g', 6, 'p', 2, 'l', 8, 'a']
>>> for (i, c) in enumerate(lista):
if isinstance(c, int):
print lista[i+1]
p
l
a
Of course, you need to definy what happens when the match is the last
element:
>>> lista = ['1', 'g', 6, 'p', 2, 'l', 8]
>>> for (i, c) in enumerate(lista):
if isinstance(c, int):
print lista[i+1]
p
l
Traceback (most recent call last):
File "<pyshell#25>", line 3, in -toplevel-
print lista[i+1]
IndexError: list index out of range
. Facundo
More information about the Python-list
mailing list