
The for/Else Statement in Python - atreadw
http://theautomatic.net/2020/07/09/the-for-else-statement-in-python/
======
bzb3
Surprising. I didn't know this construct and I would naturally assume that the
else part would execute if the loop never spun (because the list/etc was
empty)

~~~
amitutk
Yes, the else keyword is not intuitive. The best way to remember for/else is
for/nobreak

~~~
neverartful
Right! nobreak would make it much clearer.

~~~
Dagger2
It would involve adding a new keyword to the language though. I imagine there
was some desire to avoid that.

------
Doxin
Relatedly there's also a try/else, where the else block only runs if there
wasn't an exception.

All these else variants come in handy from time to time.

------
rosstex
The most common use case of for/else for me is:

for/len==0

