Fix Python – How can I make sense of the `else` clause of Python loops?

Question

Asked By – alexis

Many Python programmers are probably unaware that the syntax of while loops and for loops includes an optional else: clause:

for val in iterable:
    do_something(val)
else:
    clean_up()

The body of the else clause is a good place for certain kinds of clean-up actions, and is executed on normal termination of the loop: I.e., exiting the loop with return or break skips the else clause; exiting after a continue executes it. I know this only because I just looked it up (yet again), because I can never remember when the else clause is executed.

Always? On “failure” of the loop, as the name suggests? On regular termination? Even if the loop is exited with return? I can never be entirely sure without looking it up.

I blame my persisting uncertainty on the choice of keyword: I find else incredibly unmnemonic for this semantics. My question is not “why is this keyword used for this purpose” (which I would probably vote to close, though only after reading the answers and comments), but how can I think about the else keyword so that its semantics make sense, and I can therefore remember it?

I’m sure there was a fair amount of discussion about this, and I can imagine that the choice was made for consistency with the try statement’s else: clause (which I also have to look up), and with the goal of not adding to the list of Python’s reserved words. Perhaps the reasons for choosing else will clarify its function and make it more memorable, but I’m after connecting name to function, not after historical explanation per se.

The answers to this question, which my question was briefly closed as a duplicate of, contain a lot of interesting back story. My question has a different focus (how to connect the specific semantics of else with the keyword choice), but I feel there should be a link to this question somewhere.

Now we will see solution for issue: How can I make sense of the `else` clause of Python loops?


Answer

The while statement with an else clause

while condition:
    suite
else:
    exhausted

is exactly equivalent to

while True:
    if not condition:
        exhausted
        break
    suite

The for statement with an else clause

for item in iterable:
    suite
else:
    exhausted

is exactly equivalent to

iterator = iter(iterable)
try:
    while True:
        item = next(iterator)
        suite
except StopIteration:
    exhausted

It helps understand the effect of a break or continue statement in the suite statement.

Note. — For the while and for statements without an else clause, replace the exhausted statement with a pass statement in the equivalent code.

To memorise the meaning of the else clause, you can interpret a loop statement as

if loop_breaks:
    pass
else:
    exhausted

This question is answered By – Maggyero

This answer is collected from stackoverflow and reviewed by FixPython community admins, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0