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
🌐
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.
Discussions

What does : do in python
A line that ends with a : signals that a nesting level [edit: or "code block"] begins, meaning the following line (plus potentially more) will be indented. Typical cases are if condition: nested elif condition: nested else: nested for item in items: nested while condition: nested def function(args): nested class ClassName: nested try: nested except Exception: nested More on reddit.com
🌐 r/learnpython
13
5
May 3, 2024
"for i in range()" to do an infinite loop with a counter - Ideas - Discussions on Python.org
Hi, Usually in Python we can avoid the i = 0 … i += 1 paradigm that we use in other languages when we need to count things, thanks to enumerate(...), for i in range(100), etc. Along the years I have nearly always found a more “pythonic” replacement for code containing i = 0 … i += 1. ... More on discuss.python.org
🌐 discuss.python.org
4
August 10, 2022
Why do you choose Python over other language?

A few off the top of my head:

  • Huge standard library

  • Generally good quality documentation for standard library

  • Gobs of third-party modules to rival even Java

  • Platform agnostic, and present in virtually every *nix distribution I'm aware of without even needing to install it (because so many system tools are written using it)

  • Very common use of BSD/MIT-style licencing with third-party modules; GPL licencing gives corporate lawyers a big headache

  • Emphasis on code readability (see PEP-8) and DRY principles without sacrificing readability (kind of a middle ground between perl and java, I guess?)

  • Useful for a really broad range of programming tasks from little shell scripts to enterprise web applications to scientific uses; it may not be as good at any of those as a purpose-built programming language but it can do all of them, and do them well (e.g. you don't see web apps written as bash scripts nor do you see linux package managers written in Java)

More on reddit.com
🌐 r/Python
155
108
February 17, 2012
Why do you love Python?
It makes simple things easy and complicated things possible. But to be more specific: It's not wed to OOP like Java and you don't need a ton of boilerplate for simple programs. It's standard library has a ton of stuff I need and third party packages exist for everything else. If I am making a quick script and just want to slap something together with dictionaries and lists, I can. If I need something larger and organized I can use classes and type hints. The community is legit full of nice people. More on reddit.com
🌐 r/Python
129
283
August 27, 2021
🌐
Python documentation
docs.python.org › 3 › tutorial › modules.html
6. Modules — Python 3.14.3 documentation
A program doesn’t run any faster when it is read from a .pyc file than when it is read from a .py file; the only thing that’s faster about .pyc files is the speed with which they are loaded. The module compileall can create .pyc files for all modules in a directory. There is more detail on this process, including a flow chart of the decisions, in PEP 3147. Python comes with a library of standard modules, described in a separate document, the Python Library Reference (“Library Reference” hereafter).
🌐
freeCodeCamp
freecodecamp.org › news › python-do-while-loop-example
Python Do While – Loop Example
August 31, 2021 - 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.

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.
Find elsewhere
🌐
Wikipedia
en.wikipedia.org › wiki › Python_(programming_language)
Python (programming language) - Wikipedia
1 day ago - Python claims to strive for a simpler, less-cluttered syntax and grammar, while giving developers a choice in their coding methodology. Python lacks do .. while loops, which Rossum considered harmful. In contrast to Perl's motto "there is more than one way to do it", Python advocates an approach where "there should be one – and preferably only one – obvious way to do it".
🌐
Python
python.org › downloads
Download Python | Python.org
Python 3.14.2 Dec. 5, 2025 Download Release notes
🌐
Python
python.org
Welcome to Python.org
The official home of the Python Programming Language
🌐
Real Python
realpython.com › python-do-while
How Can You Emulate Do-While Loops in Python? – Real Python
August 2, 2022 - In this tutorial, you'll learn how to emulate do-while loops in Python. The most common technique to do this is to create an infinite while loop with a conditional statement that controls the loop and jumps out of it using a break statement.
🌐
Python documentation
docs.python.org › 3 › tutorial › controlflow.html
4. More Control Flow Tools — Python 3.14.3 documentation
Likewise, don’t use non-ASCII characters in identifiers if there is only the slightest chance people speaking a different language will read or maintain the code. ... Actually, call by object reference would be a better description, since if a mutable object is passed, the caller will see any changes the callee makes to it (items inserted into a list). ... © Copyright 2001 Python Software Foundation.
🌐
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.
🌐
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.
🌐
Python documentation
docs.python.org › 3 › library › functions.html
Built-in Functions — Python 3.14.3 documentation
2 weeks ago - If the string is the name of one of the object’s attributes, the result is the value of that attribute. For example, getattr(x, 'foobar') is equivalent to x.foobar. If the named attribute does not exist, default is returned if provided, otherwise AttributeError is raised. name need not be a Python identifier (see setattr()).
🌐
Python
peps.python.org › pep-0008
PEP 8 – Style Guide for Python Code | peps.python.org
Guido van Rossum <guido at python.org>, Barry Warsaw <barry at python.org>, Alyssa Coghlan <ncoghlan at gmail.com> ... This document gives coding conventions for the Python code comprising the standard library in the main Python distribution.
🌐
W3Schools
w3schools.com › python › python_intro.asp
Introduction to Python
Python Variables Variable Names Assign Multiple Values Output Variables Global Variables Variable Exercises Code Challenge Python Data Types
🌐
Python.org
discuss.python.org › ideas
"for i in range()" to do an infinite loop with a counter - Ideas - Discussions on Python.org
August 10, 2022 - Hi, Usually in Python we can avoid the i = 0 … i += 1 paradigm that we use in other languages when we need to count things, thanks to enumerate(...), for i in range(100), etc. Along the years I have nearly always found a more “pythonic” replacement for code containing i = 0 … i += 1. There is an exception with this code: an infinite loop with a counter: i = 0 while True: ... if breaking_condition: break i += 1 Proposal: could we accept that range() without any parameter ...
🌐
Visual Studio Code
code.visualstudio.com › docs › python › python-tutorial
Getting Started with Python in VS Code
November 3, 2021 - You can also work with variables in the Debug Console (If you don't see it, select Debug Console in the lower right area of VS Code, or select it from the ... menu.) Then try entering the following lines, one by one, at the > prompt at the bottom of the console: ... Select the blue Continue button on the toolbar again (or press F5) to run the program to completion. "Roll a dice!" appears in the Python Debug Console if you switch back to it, and VS Code exits debugging mode once the program is complete.
🌐
W3Schools
w3schools.com › python › python_for_loops.asp
Python For Loops
A for loop is used for iterating over a sequence (that is either a list, a tuple, a dictionary, a set, or a string). This is less like the for keyword in other programming languages, and works more like an iterator method as found in other ...
🌐
Python
python.org › downloads › release › python-31210
Python Release Python 3.12.10 | Python.org
Python 3.12.10 is the latest maintenance release of Python 3.12, and the last full maintenance release. Subsequent releases of 3.12 will be security-fixes only. This last maintenance release contains about 230 bug fixes, build improvements and documentation changes since 3.12.9.