Basically, I need a way to return control to the beginning of a for loop and actually restart the entire iteration process after taking an action if a certain condition is met.
What I’m trying to do is this:
for index, item in enumerate(list2):
if item == '||' and list2[index-1] == '||':
del list2[index]
*<some action that resarts the whole process>*
That way, if [‘berry’,’||’,’||’,’||’,’pancake] is inside the list, I’ll wind up with:
[‘berry’,’||’,’pancake’] instead.
Thanks!
I’m not sure what you mean by “restarting”. Do you want to start iterating over from the beginning, or simply skip the current iteration?
If it’s the latter, then
forloops supportcontinuejust likewhileloops do:The above will print the numbers from 0 to 9, except for 5.
If you’re talking about starting over from the beginning of the
forloop, there’s no way to do that except “manually”, for example by wrapping it in awhileloop:The above will print the numbers from 0 to 5, then start over from 0 again, and so on indefinitely (not really a great example, I know).