I am not sure what you are trying to do. You can implement a do-while loop like this:

while True:
  stuff()
  if fail_condition:
    break

Or:

stuff()
while not fail_condition:
  stuff()

What are you doing trying to use a do while loop to print the stuff in the list? Why not just use:

for i in l:
  print i
print "done"

Update:

So do you have a list of lines? And you want to keep iterating through it? How about:

for s in l: 
  while True: 
    stuff() 
    # use a "break" instead of s = i.next()

Does that seem like something close to what you would want? With your code example, it would be:

for s in some_list:
  while True:
    if state is STATE_CODE:
      if "//" in s:
        tokens.add( TOKEN_COMMENT, s.split( "//" )[1] )
        state = STATE_COMMENT
      else :
        tokens.add( TOKEN_CODE, s )
    if state is STATE_COMMENT:
      if "//" in s:
        tokens.append( TOKEN_COMMENT, s.split( "//" )[1] )
        break # get next s
      else:
        state = STATE_CODE
        # re-evaluate same line
        # continues automatically
Answer from Tom on Stack Overflow
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python โ€บ python-do-while
Python Do While Loops - GeeksforGeeks
July 23, 2025 - In Python, we can simulate the behavior of a do-while loop using a while loop with a condition that is initially True and then break out of the loop when the desired condition is met.
Discussions

As a beginner how do I understand while loops?
Itโ€™s just a loop which continues running until the condition becomes false. More on reddit.com
๐ŸŒ r/learnpython
76
36
April 10, 2025
How to learn for loop, do while loop and functions?
It seems like you have started learning python from the data analysis point of view, using a Jupyter notebook for data research and processing. Python can also be used as a programming language to create applications and scripts, you will find functions loops and other structures there more. I'd recommend trying to learn python from that point of view, hopefully that will help you complete the picture and understand it all better. Try this free online book: https://automatetheboringstuff.com/#toc More on reddit.com
๐ŸŒ r/learnpython
5
3
March 20, 2023
While Loops, how do they work?
Iโ€™m not new to python as a whole but Iโ€™m very bad at it however, I want to learn. Iโ€™ve tried to do some coding but I want to know what everything is actually doing rather than just reciting things online so if some of you could explain it to me as if I was like a baby that would make ... More on discuss.python.org
๐ŸŒ discuss.python.org
8
0
June 4, 2026
loops - Is there a "do ... until" in Python? - Stack Overflow
Python is not a free-form language and this kind for statement won't layout nicely with only indentation. You certainly want the condition to appear at the end of the block, otherwise, it's highly ambiguous. ... Save this answer. ... Show activity on this post. There is no do-while loop in Python. More on stackoverflow.com
๐ŸŒ stackoverflow.com
๐ŸŒ
freeCodeCamp
freecodecamp.org โ€บ news โ€บ python-do-while-loop-example
Python Do While โ€“ Loop Example
August 31, 2021 - There are cases where you would want your code to run at least one time, and that is where do while loops come in handy. For example, when you're writing a program that takes in input from users you may ask for only a positive number. The code will run at least once. If the number the user submits is negative, the loop will keep on running. If it is positive, it will stop. Python does not have built-in functionality to explicitly create a do while loop like other languages.
๐ŸŒ
Stanford CS
cs.stanford.edu โ€บ people โ€บ nick โ€บ py โ€บ python-while.html
While Loop
Given that we have if/break to get out of the loop, it's possible to get rid of the test at the top of the loop entirely, relying on if/break to end the loop. We do this by writing the loop as while True:...
๐ŸŒ
W3Schools
w3schools.com โ€บ PYTHON โ€บ python_while_loops.asp
Python While Loops
Python Examples Python Compiler ... Python Bootcamp Python Training ... With the while loop we can execute a set of statements as long as a condition is true....
๐ŸŒ
Texas Instruments
education.ti.com โ€บ en โ€บ resources โ€บ computer-science-foundations โ€บ do-while-loops
Python coding: Do While Loops | Texas Instruments
Explore Do While loops in Python on the TI-Nspireโ„ข CX II graphing calculator. Do While loops are a form of post-test loop that always run at least one time.
Find elsewhere
๐ŸŒ
Coursera
coursera.org โ€บ tutorials โ€บ python-while-loop
How to Write and Use Python While Loops | Coursera
Even if the condition is never true, the statements inside the loop body will be executed at least once. do while loops are useful when you need your code to run at least once. Python does not have a built-in do while loop like other programming ...
๐ŸŒ
DataCamp
datacamp.com โ€บ tutorial โ€บ do-while-loop-python
Emulating a Do-While Loop in Python: A Comprehensive Guide for Beginners | DataCamp
January 31, 2024 - Python does not have a built-in "do-while" loop, but you can emulate its behavior.
๐ŸŒ
YoungWonks
youngwonks.com โ€บ blog โ€บ do-while-loop-python
What is a do while loop in Python? How do I create a while loop in Python? What is the difference between a for loop and a while loop in Python?
February 18, 2024 - A do-while loop is a loop that executes a block of code at least once, then checks a boolean condition which results in a true or false to decide whether to repeat the block of code or not.
๐ŸŒ
YouTube
youtube.com โ€บ alex the analyst
While Loops in Python | Python for Beginners - YouTube
Take my Full Python Course Here: https://bit.ly/48O581RIn this series we will be walking through everything you need to know to get started in Python! In thi...
Published ย  November 29, 2022
Views ย  54K
๐ŸŒ
Medium
medium.com โ€บ @BetterEverything โ€บ until-loops-and-do-while-loops-in-python-this-is-how-bfe43b22e6b4
Until Loops and Do While Loops in Python? This is how! | by Strive to Develop | Medium
August 13, 2024 - Other languages have a Do While loop. A Do While loop runs its code at least once by checking the while condition only at the end. In Python I have never seen an Until loop and until is not a special keyword.
๐ŸŒ
Real Python
realpython.com โ€บ python-while-loop
Python while Loops: Repeating Tasks Conditionally โ€“ Real Python
March 3, 2025 - However, translating the for loop ... ... A do-while loop is a control flow statement that executes its code block at least once, regardless of whether the loop condition is true or false....
๐ŸŒ
Reddit
reddit.com โ€บ r/learnpython โ€บ how to learn for loop, do while loop and functions?
r/learnpython on Reddit: How to learn for loop, do while loop and functions?
March 20, 2023 -

I work as an Analyst, have some experience with Python, like using groupby, filter conditions, joins, windows function, rank function to basically get the business logic to code. I see people who are efficient in coding tend to write their code as a single function rather than going through each step one at a time like cells in jupyter notebook. I want to learn how can I be better at writing loops, conditions and functions together as a whole. Like for example my coworker built a function where he declared a empty list outside the function, use it within, used counters and basically ran a groupby with agg to include the function fetching the desired output as a dataframe. How will I be able to work on such complex functions using loops and other parameters?

๐ŸŒ
Python documentation
docs.python.org โ€บ 3 โ€บ tutorial โ€บ controlflow.html
4. More Control Flow Tools โ€” Python 3.14.6 documentation
The pass statement does nothing. It can be used when a statement is required syntactically but the program requires no action. For example: >>> while True: ...
๐ŸŒ
YouTube
youtube.com โ€บ watch
How To Emulate A Do While Loop | Python Example - YouTube
How to emulate the behaviour of a do-while loop in Python even though the language does not officially include do-while loops. Source code: https://github.c...
Published ย  January 30, 2023
๐ŸŒ
Python.org
discuss.python.org โ€บ python help
While Loops, how do they work? - Python Help - Discussions on Python.org
June 4, 2026 - Iโ€™m not new to python as a whole but Iโ€™m very bad at it however, I want to learn. Iโ€™ve tried to do some coding but I want to know what everything is actually doing rather than just reciting things online so if some of you could explain it to me as if I was like a baby that would make ...
๐ŸŒ
Programiz
programiz.com โ€บ python-programming โ€บ while-loop
Python while Loop (With Examples)
In Python, we use a while loop to repeat a block of code until a certain condition is met.