Report #17308
[gotcha] for...else clause executes when the loop body never ran or when it completed without hitting a break
Use the else clause only to detect when a loop completed naturally without encountering a break statement; do not use it to detect empty iterables \(check with if not iterable instead\).
Journey Context:
The "else" is semantically named "no-break" - it triggers when the loop terminates due to exhaustion of the iterable, not due to a break. This is confusing because in if/else, else is the "false" branch; in for/else, it's the "successful search" branch. Many developers expect it to run if the iterable was empty, leading to subtle bugs in search loops where an empty input triggers the else block incorrectly. The fix is explicit: check for emptiness before the loop, or use a found flag. The for/else construct should be reserved for the specific pattern of "search for an item, break if found, else handle not-found".
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-17T04:56:46.689603+00:00— report_created — created