This article discusses Python's for...else and while...else syntax, one of the most common and misunderstood grammatical features in Python.
A for, while loop in Python has an optional else branch (like an if statement and a try statement) that executes after the loop iteration completes normally. In other words, if we are not exiting the loop in any way other than the normal way, then the else branch will be executed. That is, there is no break statement, no return statement, or no exception appearing in the loop body. Consider a simple (useless) example:
>>> for I in range (5):
... Print (i)
... else:. Print (' iterated over everything ')
...
0
1
2
3
4
iterated over everything
In the code above, we iterate over the range (5) and print each number. Because we allow the loop to complete normally, the Else branch is also executed and prints out the iterated over everything:). Conversely, if we terminate the loop with a break statement, the else branch will not execute:
>>> for I in range (5):
... if i = = 2:
... Break
... Print (i)
... else:. Print (' iterated over everything ')
...
0
1
Note that although the sequence of iterations of the loop is empty, the Else branch is still executed, after all, the loop is still done properly.
>>> for i in []: ... Print (i)
... else:. Print (' Still iterated over everything (i.e. nothing) ')
...
Still iterated over everything (i.e)
Also do not forget that all of the above are also adapted to While...else:
>>> i = 0
>>> While I <= 5:
... i + 1
... Print I ...
else:
.... print ' Yep '
...
1
2
3
4
5
Yep
But, why!?
A common use case of an else statement in a loop is to implement a circular lookup. The hypothesis is that you are looking for a project (item) that satisfies a specific condition, and that you need to do additional processing, or generate an error if an acceptable value is not found:
For x in data:
if Meets_condition (x):
break
else:
# Raise the error or do additional processing
Without the Else statement, you need to set a flag and then examine it later to determine whether there is a value that satisfies the condition.
Condition_is_met = False for
x in data:
if Meets_condition (x):
Condition_is_met = True
if not condition _is_met:
# Raise error or do additional processing
This is not a really important thing, and in many other languages you have to do so. But like many other features of Python, else statements can generate more elegant Python-style (pythonic) code. There is no doubt in the above example that using the Else statement makes the code more Zen of Python friendly:
This is not to say that you have to use the Else statement in the loop, you can always use the flags and so on. But the Else statement often makes the code more elegant and more readable. You may think this is pythonic and make the intention clearer (hi!), while others may think this is confusing and redundant! Personally, I insist on using the Else statement in the loop unless there is another more readable method (I think, for me, the readability of the code is the most important).