Skip to main content

How to Skip Iterations in a For Loop in Python

When iterating through a sequence in Python, there are situations where you want to bypass certain elements: for example, skipping invalid entries, filtering out specific values, or ignoring items that don't meet a condition. Python provides the continue statement specifically for this purpose. This guide explains how to skip iterations in a for loop using continue, shows alternative approaches with if-else, and clarifies the difference between continue and break.

Using continue to Skip Iterations

The continue statement tells Python to stop executing the current iteration and immediately jump to the next iteration of the loop. Any code after continue in the loop body is skipped for that particular iteration.

fruits = ["apple", "banana", "cherry", "date", "elderberry"]

for fruit in fruits:
if fruit == "cherry":
continue # Skip "cherry"
print(fruit)

Output:

apple
banana
date
elderberry

When fruit equals "cherry", the continue statement is triggered. Python skips the print(fruit) line for that iteration and moves directly to "date".

How continue Works Step by Step

Iteration 1: fruit = "apple"   -> condition False -> prints "apple"
Iteration 2: fruit = "banana" -> condition False -> prints "banana"
Iteration 3: fruit = "cherry" -> condition True -> continue (skip print)
Iteration 4: fruit = "date" -> condition False -> prints "date"
Iteration 5: fruit = "elderberry" -> condition False -> prints "elderberry"

Skipping Based on a Numeric Condition

A common use case is filtering numbers in a range. For example, to print only even numbers by skipping odd ones:

for i in range(1, 11):
if i % 2 != 0: # Skip odd numbers
continue
print(i)

Output:

2
4
6
8
10

Every time i is odd (i % 2 != 0), continue skips the print() call, and the loop advances to the next number.

Skipping Multiple Conditions

You can skip iterations based on more than one condition using logical operators:

for i in range(1, 21):
# Skip numbers divisible by 3 or 5
if i % 3 == 0 or i % 5 == 0:
continue
print(i, end=" ")

Output:

1 2 4 7 8 11 13 14 16 17 19

Numbers like 3, 5, 6, 9, 10, 12, 15, 18, and 20 are all skipped because they are divisible by 3, 5, or both.

Using if-else as an Alternative to continue

If you need to execute different logic for the skipped and non-skipped cases, an if-else block is more appropriate than continue:

for num in range(1, 6):
if num % 2 == 0:
print(f" Skipping {num} (even)")
else:
print(f" Processing {num} (odd)")

Output:

  Skipping 2 (even)
Skipping 4 (even)
Processing 1 (odd)
Processing 3 (odd)
Processing 5 (odd)

With continue, the skipped iterations are silent: no code runs for them. With if-else, you can log, count, or handle skipped items explicitly.

tip

Use continue when skipped iterations need no action at all. Use if-else when you want to handle both cases: for example, logging which items were skipped and which were processed.

Common Mistake: Using break Instead of continue

A frequent error is using break when the intent is to skip a single iteration. The break statement terminates the entire loop, not just the current iteration:

names = ["Alice", "Bob", "Charlie", "Diana"]

# WRONG: break stops the loop entirely at "Bob"
for name in names:
if name == "Bob":
break
print(name)

Output:

Alice

The loop stops completely when it encounters "Bob", so "Charlie" and "Diana" are never reached.

The correct approach to skip "Bob" and continue processing the rest:

names = ["Alice", "Bob", "Charlie", "Diana"]

# CORRECT: continue skips "Bob" and processes the rest
for name in names:
if name == "Bob":
continue
print(name)

Output:

Alice
Charlie
Diana
danger

break exits the loop. continue skips the current iteration. Confusing the two can cause your loop to terminate prematurely, silently dropping all remaining items.

Skipping Iterations in Nested Loops

When using continue inside nested loops, it only affects the innermost loop that contains it:

for i in range(1, 4):
for j in range(1, 4):
if j == 2:
continue # Skips j=2, but outer loop continues normally
print(f"i={i}, j={j}")

Output:

i=1, j=1
i=1, j=3
i=2, j=1
i=2, j=3
i=3, j=1
i=3, j=3

Every combination where j=2 is skipped, but the outer loop over i is unaffected.

Real-World Example: Skipping Invalid Data

A practical scenario is processing a list of records where some entries are invalid:

records = [
{"name": "Alice", "age": 30},
{"name": "", "age": 25}, # Invalid: empty name
{"name": "Charlie", "age": -1}, # Invalid: negative age
{"name": "Diana", "age": 28},
{"name": None, "age": 22}, # Invalid: None name
]

valid_count = 0

for record in records:
# Skip records with missing or empty names
if not record["name"]:
continue

# Skip records with invalid ages
if record["age"] < 0:
continue

print(f"Processing: {record['name']}, age {record['age']}")
valid_count += 1

print(f"\nTotal valid records processed: {valid_count}")

Output:

Processing: Alice, age 30
Processing: Diana, age 28

Total valid records processed: 2

Three records are skipped: one with an empty name, one with a negative age, and one with None as the name.

Quick Reference

StatementBehaviorUse When
continueSkips the rest of the current iteration and moves to the nextYou want to bypass specific items but keep looping
breakExits the loop entirelyYou want to stop the loop when a condition is met
passDoes nothing (placeholder)You need an empty block that doesn't affect the loop
if-elseExecutes different code for each caseYou need to handle both skipped and processed items

The continue statement is the standard and most readable way to skip iterations in a Python for loop. It keeps your code clean by avoiding deep nesting and makes the intent: "skip this item and move on": immediately clear to anyone reading your code.