How to start a loop again in python
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. Show What I'm trying to do is this:
That way, if ['berry','||','||','||','pancake] is inside the list, I'll wind up with: ['berry','||','pancake'] instead. Thanks! asked Sep 13, 2010 at 22:34
GeorginaGeorgina 3811 gold badge6 silver badges13 bronze badges 6 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
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
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). answered Sep 13, 2010 at 22:44
Liquid_FireLiquid_Fire 6,8802 gold badges24 silver badges22 bronze badges 3
That will do what you seem to want. Why you would want to do it is a different matter. Maybe you should take a look at your code and make sure you're not missing an obvious and easier way to do it. answered Sep 13, 2010 at 22:44
nmichaelsnmichaels 48.1k12 gold badges102 silver badges132 bronze badges 1
A poor way to think of an algorithm. You're just filtering, i.e., removing duplicates. And -- in Python -- you're happiest making copies, not trying to do
answered Sep 13, 2010 at 22:56
S.LottS.Lott 376k78 gold badges503 silver badges771 bronze badges 5 The inevitable itertools version, because it just came to me:
answered Sep 13, 2010 at 23:26
Jochen RitzelJochen Ritzel 102k29 gold badges195 silver badges190 bronze badges 1
answered Sep 13, 2010 at 22:37
voltingvolting 15k7 gold badges34 silver badges54 bronze badges 2
answered Sep 13, 2010 at 22:39
Sam DolanSam Dolan 31.3k9 gold badges84 silver badges84 bronze badges 1 As you can see answering your question leads to some rather convoluted code. Usually a better way can be found, which is why such constructs aren't built into the language If you are not comfortable using itertools, consider using this loop instead. Not only is it easier to follow than your restarting for loop, it is also more efficient because it doesn't waste time rechecking items that have already been passed over.
answered Sep 13, 2010 at 23:45
John La RooyJohn La Rooy 286k51 gold badges358 silver badges498 bronze badges
And by the way this is repeating of Remove adjacent duplicate elements from a list
answered Sep 13, 2010 at 23:32
How do you go back to the start of a loop in Python?The continue statement in Python returns the control to the beginning of the while loop. The continue statement rejects all the remaining statements in the current iteration of the loop and moves the control back to the top of the loop. The continue statement can be used in both while and for loops.
How do I start a while loop again?You use the continue statement to restart a loop such as the while loop, for loop or for-in loop. If there are nested loops, the continue statement will restart the innermost loop.
How do I run a program again in Python?Let us see in the below example code the usage of the while conditional operator to loop back to the beginning of the program using Python. again = input("Do you want to Exit this program.") if again not in {"y","n"}: print("Enter y to exit or n to continue.")
|