Skip to main content

How to Skip the Rest of a Loop Turn in Python with continue

· 3 min read
Jagdish Kumawat
Founder @ Dewiride

Inside a loop, some items shouldn't be processed: an empty line, a blank answer. continue skips the rest of the current turn and moves on to the next item, so the code that follows doesn't need to sit inside an if. This guide skips empty strings, with real output from Python 3.15.2.

Syntax​

Syntax
for <item> in <iterable>:
if <condition>:
continue
<code for the items you keep>
PartWhat it means
if <condition>:The check that decides to skip
continueEnds this turn now and starts the next one
The code belowRuns only for items that weren't skipped

continue works the same in a while loop. Python's language reference: "continue may only occur syntactically nested in a for or while loop, but not nested in a function or class definition within that loop. It continues with the next cycle of the nearest enclosing loop."

Prerequisites​

  • uv and Python. This guide uses uv 0.13.10 with Python 3.15.2, in a project made with uv init.

Step 1: Write the Script​

skip_empty.py
for word in ["apple", "", "pear", "", "plum"]:
if not word:
continue
print(word.upper())

The skip_empty.py script in a macOS-style code window: a for loop over five strings, two of them empty, skips the empty ones with continue and prints the others in capitals.

not word is true for an empty string.

Step 2: Run It​

Terminal
uv run python skip_empty.py
Output
APPLE
PEAR
PLUM

The output in a terminal: APPLE, PEAR and PLUM, each on its own line.

The loop ran five times but printed three lines: on the two empty strings, continue skipped the print().

Common Mistakes​

MistakeWhat happensDo this instead
Using continue outside a loopSyntaxError: 'continue' not properly in loopUse it only inside for or while
Using break when you meant continueThe loop ends at the first empty stringcontinue skips one turn; break ends the loop
In a while loop, updating the counter below continueThe counter never changes, and the loop repeats foreverUpdate it before continue

Troubleshooting​

SymptomLikely causeFix
Items you expected are missingThe skip condition is too broadPrint the items the condition skips, then narrow it

FAQ​

Is continue better than wrapping the code in an if?​

It keeps the main code unindented, which helps when several checks come before it. For a single short check, an if reads just as well.

Does continue skip the loop's else block?​

No. A loop's else runs when the loop finishes without break, whether or not continue was used.

Conclusion​

On the two empty strings, continue jumped to the next item, so the loop printed only APPLE, PEAR and PLUM.

Additional Resources​