The Loop else Clause

A little-known Python feature โ€” a loop can have an else block, and it only skips when the loop was cut short by break.

Overview

Both for and while loops in Python can carry an optional else block. It's one of Python's more surprising features, because else here doesn't mean "if the loop's condition was false" the way you might expect from if/else โ€” it means "if the loop finished without hitting a break."

๐Ÿ“Œ This topic is optional. It's a nice-to-know Python quirk rather than something you'll need constantly โ€” most codebases avoid it because it reads unintuitively to newcomers. Good to recognize when you see it, though.

Key Concepts

The Core Idea

for item in sequence:
    if condition:
        break
else:
    # runs only if the loop never hit break
    print("Loop finished without breaking")
๐Ÿ’ก A useful way to remember it: "else runs unless you break."

Example 1 โ€” for...else, no break (else runs)

for i in range(5):
    print(i)
else:
    print("Loop completed!")

Output:

0
1
2
3
4
Loop completed!

The loop ran through every value of i without ever hitting a break, so the else block executes right after.

Example 2 โ€” for...else, with break (else is skipped)

for i in range(5):
    if i == 3:
        print("Found 3, stopping early")
        break
    print(i)
else:
    print("Loop completed!")

Output:

0
1
2
Found 3, stopping early

As soon as i == 3, break fires and the loop exits immediately โ€” "Loop completed!" never prints, because the loop didn't finish on its own.

Example 3 โ€” Practical use: searching for a value

This is the classic real use case โ€” checking whether something was found during a search:

numbers = [4, 8, 15, 16, 23, 42]
target = 15

for n in numbers:
    if n == target:
        print(f"Found {target}!")
        break
else:
    print(f"{target} was not in the list.")

Output:

Found 15!

If target had been, say, 99 (not in the list), the loop would run to completion without ever hitting break, and the else block's "was not in the list" message would print instead.

Example 4 โ€” while...else

count = 0
while count < 3:
    print(count)
    count += 1
else:
    print("While loop finished normally")

Output:

0
1
2
While loop finished normally

Same rule applies to while: since the loop ended because its condition became False (not because of a break), the else block runs.

Why This Feature Exists

Without else, checking "did my loop find something or not" usually needs an extra flag variable:

# Without loop-else โ€” needs a manual flag
found = False
for n in numbers:
    if n == target:
        found = True
        break
if not found:
    print(f"{target} was not in the list.")

The for...else version in Example 3 achieves the same result without the extra found variable โ€” the loop's own completion state does the job.

Important Points

Things to Remember

Quick Revision

A loop's else block runs only when the loop finishes without hitting a break โ€” think "else runs unless you break." It works on both for and while loops, and it's most commonly used for search patterns where you want to react differently depending on whether the item was found (break fired) or the whole sequence was searched with no match (else fired). continue doesn't affect it โ€” only break does.