I need to go to the next element of my for if a certain condition is true and restart my cycle from that element.
sets = [0,1,2,3]
for elem in sets:
if (elem == 0)
#next elem?
....
....
There is nothing you have to do to go to the next element. The loop automatically iterates when the nested code has finished executing.
Example:
for elem in sets:
if elem == 0:
print(element)
will print all the elements that meet the condition; the sets
will be exhausted.
Now, to force the loop to iterate even if the nested block has not finished executing yet, you can use continue
.
Example:
for elem in sets:
if elem == 0:
print('found a 0!')
continue
print('Do you see me?')
In this case, whenever a 0
is found, the loop will terminate prematurely (without the 'Do you see me?'
being print).
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments