I have the following list:
lista = [1,2,3,5,0,5,6,0]
I know that print(lista.index(0))
will print the first index where it found the number i.e. 4
How do I get it to print the next index which should be 7 etc?
I have the following list:
lista = [1,2,3,5,0,5,6,0]
I know that print(lista.index(0))
will print the first index where it found the number i.e. 4
How do I get it to print the next index which should be 7 etc?
The classic way is to build a list of the indices:
eg:
>>> indices = [i for i, v in enumerate(a) if v == 0]
>>> print indices
[4, 7]
Of course - this can be turned into a generator expression instead. But in short - using enumerate
is what you're after.