Something that is not common knowledge is the fact that Python for loop can have an else
clause.
An else clause is executed when a for loop ends normally without break being called.
Internally, Python will trigger a StopIteration
when this happens.
def check(target, iterable):
for element in iterable:
if element == target:
print("Jup, target is in container.")
break
else:
print("Seems like target is not in container.")
check("s", "a string")
# >>> Jup, target is in container.
check("e", "a string")
# >>> Seems like target is not in container.
Such an else clause may save you some lines of codes when itterating in the middle of a complex function by getting rid of assignment checks or boolean flags.
However, in the simplified example above using return
instead of break
and else
would most likely produce code that is more robust and easier to comprehend as for/else
is not a well-known construct.