Codectionary / Developer documentation / Python

else on for and while loops

Express what happens when a loop finishes without taking a break.

How it works

A loop else clause runs after normal completion: a for loop exhausts its iterable, or a while condition becomes false. A break skips the else clause.

The else belongs to the loop at the same indentation level, not to an inner if. An empty for loop can execute its else immediately. Returning from the function or propagating an exception also prevents reaching the loop else.

Limitations and practical choices

Use loop else when it clearly expresses a search that failed. A helper returning a result may be easier to read for more complex searches.

continue does not suppress else: it only moves to the next iteration. The important distinction is normal completion versus leaving via break.

Try it yourself

Search a list for the first negative number. Test a match, no match and an empty list. Explain which paths execute else.

Syntax

for item in items:
    if matches(item):
        break
else:
    print("No match")

Examples

Handle a failed search

Print a message only when no item matched.

names = ["John", "James"]
for name in names:
    if name == "Sam":
        print("Found", name)
        break
else:
    print("No matching name")
  1. Neither comparison succeeds, so the iterable is exhausted. The loop’s else then runs.
  2. There is no separate found flag to keep consistent with the search. A successful break would bypass the message.
  3. Use this for a short validation or lookup over a small iterable.
  4. Expected output: No matching name

Stop on the first match

Confirm that break prevents the else branch.

for value in [2, -1, -3]:
    if value < 0:
        print("First negative:", value)
        break
else:
    print("No negatives")
  1. Two does not match. Minus one matches, is printed, and ends the loop with break.
  2. Minus three is not examined and the else message is skipped. The output describes the first match, not every match.
  3. Use a complete loop without break when you need all validation errors rather than the first one.
  4. Expected output: First negative: -1

Complete a countdown

See a while loop finish through its condition.

remaining = 2
while remaining > 0:
    print(remaining)
    remaining -= 1
else:
    print("Finished")
  1. Each iteration prints the current positive number and subtracts one. Eventually the condition becomes false.
  2. No break occurred, so else prints Finished. The same else would also run if remaining started at zero.
  3. A completion branch can distinguish exhausting attempts from an early successful break, when that distinction is useful.
  4. Expected output: 2 1 Finished

Best practices

  • Use loop else when it clearly expresses a search that failed. A helper returning a result may be easier to read for more complex searches.
  • continue does not suppress else: it only moves to the next iteration. The important distinction is normal completion versus leaving via break.

Runtime & versions

Python interpreter. Feature availability depends on the language and runtime version used by your project; consult the official reference below.

Python language reference

Specifications & further reading