[SOLVED] update the iterator while looping through a list


This Content is from Stack Overflow. Question asked by Leimomi Bong

Newbie question… Looping through a list, and I want to update the iterator with the value at each index until I find the value ‘0’. Then return the index ‘0’ is located at.

I think it’s similar to a “sidewinder” algo, but I’m really unfamiliar!

here’s a photo of what i’m trying to do:

here's a photo of what's i'm trying to do


I am not quite sure what result you expect, but is this code satisfy your question?

l = [2,4,3,1,0,7,8,0,9]
i = 0

for idx, val in enumerate(l):
    idx = val
    if val == 0:
        result = l.index(val)
    print(f'index: {idx} --- value: {val}')


This Question was asked in StackOverflow by Leimomi Bong and Answered by Coderio It is licensed under the terms of CC BY-SA 2.5. - CC BY-SA 3.0. - CC BY-SA 4.0.

people found this article helpful. What about you?