How can I do this in python:
x = [1,2,3,4,5,6]
for i in x:
if i == 4:
-restart the loop from beginning-
else:
print i
So here it will print till 4 then repeat the loop
What about this:
x = [1,2,3,4,5,6]
restart = True
while restart:
for i in x:
# add any exit condition!
# if foo == bar:
# restart = False
# break
if i == 4:
break
else:
print i
print i before the if statement, and remove elseI would use a recursive function for that
def fun(x):
for i in x:
if i == 4:
fun(x)
else:
print i
return;
x = [1,2,3,4,5,6]
fun(x)
RuntimeError for exceeding the maximum recursion limit very quickly. The default is 1000.
itertools.cycle.while True: for i in range(1, 5):?