Why does python use 'else' after for and while loops?
Asked Answered
T

25

752

I understand how this construct works:

for i in range(10):
    print(i)

    if i == 9:
        print("Too big - I'm giving up!")
        break
else:
    print("Completed successfully")

But I don't understand why else is used as the keyword here, since it suggests the code in question only runs if the for block does not complete, which is the opposite of what it does! No matter how I think about it, my brain can't progress seamlessly from the for statement to the else block. To me, continue or continuewith would make more sense (and I'm trying to train myself to read it as such).

I'm wondering how Python coders read this construct in their head (or aloud, if you like). Perhaps I'm missing something that would make such code blocks more easily decipherable?


This question is about the underlying design decision, i.e. why it is useful to be able to write this code. See also Else clause on Python while statement for the specific question about what the syntax means.

Trunkfish answered 2/4, 2012 at 16:18 Comment(17)
You might like to translate it to "then" in your head.Lemnos
Don't forget the key line in the Zen of Python: "... that way may not be obvious at first unless you're Dutch."Cenis
In my head I translate it into "if not break". And, since break is used a lot in "I've found it" loops, you can translate it to "if not found", which is not far from what else readsBonita
@MestreLion: Well, but you should also add "... or if continue", and then it is really strange.Bret
@Bret Then the continue in the last loop run has to be hit before the break. So the break was not reached.Quadrivalent
This for...else construct is useful at least for the cases where you want to implement get_or_create() type of operations, i.e. looping through a data structure and get the first match if none was found then create a new entry in the data structure.Stepchild
I think the real question many people have here is "What's the difference between for ... else foo() and just putting foo() after the for loop?" And the answer is that they behave differently only if the loop contains a break (as described in detail below).Lacteal
What I wasn't sure about :>>> for i in []: ... pass ... else: ... print 'else will run on empty list' ... else will run on empty list - save next one's 5 minutesLeathery
It's almost like else if not break or (equivalently) else if not (condition raising break)Hadhramaut
This is an excellent explanation: python-notes.curiousefficiency.org/en/latest/python_concepts/…Dewar
To add another thought: usage of else in a for...else is consistent with the optional else clause in a try...except. Whether or not it's intuitive, at least it's consistent.Canopus
Related post - Else clause on Python while statementAnesthesiologist
Almost 10 years after this question has been asked and after programming with Python for 3 years, this is the first time I've seen the "for-else", "while-else" construct. I hope its low frequency use declines further into evaporation. The world is full of junior programmers that will "un-indent" the "else" to "fix" the bug they found.Definiendum
@Bonita Please add your comment about "if not break" as an answer. To me your comment was 1000x better than all answers.Ramah
its more like ``` elif not for: #do something after looping #and then some... ```Uredium
It becomes much more intuitive if you view it not as forelse, but breakelse.Veinlet
The else: makes more sense if you mentally translate break as found!Switchblade
V
403

It's a strange construct even to seasoned Python coders. When used in conjunction with for-loops it basically means "find some item in the iterable, else if none was found do ...". As in:

found_obj = None
for obj in objects:
    if obj.key == search_key:
        found_obj = obj
        break
else:
    print('No object found.')

But anytime you see this construct, a better alternative is to either encapsulate the search in a function:

def find_obj(search_key):
    for obj in objects:
        if obj.key == search_key:
            return obj

Or use a list comprehension:

matching_objs = [o for o in objects if o.key == search_key]
if matching_objs:
    print('Found {}'.format(matching_objs[0]))
else:
    print('No object found.')

It is not semantically equivalent to the other two versions, but works good enough in non-performance critical code where it doesn't matter whether you iterate the whole list or not. Others may disagree, but I personally would avoid ever using the for-else or while-else blocks in production code.

See also [Python-ideas] Summary of for...else threads

Vanzandt answered 2/4, 2012 at 16:30 Comment(15)
The list comprehension is the wrong one-liner. If you're looking for a single item, as in the for loop examples, and want to use a generator expression / list comprehension, then you want next((o for o in objects if o.key == search_key), None) or wrap it in a try / except and use no default value instead of an if / else.Ouachita
and like Lance Helsten's answer, there are actual cases where it's better to use a for/else construct.Obel
Cheers. I had a badly indented file where an else got paired with a for and I had no idea that was legal.Psycholinguistics
The for...else construct is useful at least for the cases where you want to implement get_or_create() type of operations, i.e. looping through a data structure and get the first match if none was found then create a new entry in the data structure.Stepchild
I think that the for loop is the most obvious of the constructs there.Pernik
It is worth mentioning that the else clause will run even if the for loop has values unless a break statement is explicity run as in this example. From the docs above: "The else clause has another perceived problem: if there is no break in the loop, the else clause is functionally redundant.". e.g. for x in [1, 2, 3]:\n print x\n else:\n print 'this executes due to no break'V
In the list-comprehension, there is no break !Sequacious
it's strange if you don't think of the case but i don't find it to be that strange. the case for not having something to loop through is common. semantically for beginners or non-programmers, though, it's probably strange.Who
For the function version, should there be print('No object found.') as the last line of the function?Neglect
I'm happy that I didn't read "find some item in the iterable, else if none was found do ..." before actually knowing how for-else works because this would horribly mislead me how for-else works. else executes when loop ends naturally (i.e. w/o break) and the "find" part is false for regular for-else loop w/o if and break statements which you added in your code.Ikkela
""find some item in the iterable, else if none was found do ..." this is wrong. We iterate for lots of reasons other than "finding things".Rating
Am I right in thinking that the else block of a for-loop only executes if the for-loop completes running THROUGH THE WHOLE TARGET ITERABLE? I assume this syntax is extremely suited for the case of a for loop that terminates or breaks once a target is found. Assuming the loop is coded to break when a target is found, it is logical that when the loop completes without being interrupted, no item was found and the search condition is False.Tabriz
I agree with several previous comments that this answer is misleading and/or partially incorrect. The answer by @lance-helsten is spot on and should be the accepted answer.Demivolt
@Rating No, it is not wrong. The for-else idiom always implements a search with a fallback in case the search fails. For loops are indeed used for lots of reasons, but that is not what this question or answer is about.Vanzandt
If it's only necessary to determine whether there is a match, rather than actually reporting the match, using any is simpler than the search function. In 3.8 and above, any can also report the first match using the walrus operator (:=).Georgetown
H
888

A common construct is to run a loop until something is found and then to break out of the loop. The problem is that if I break out of the loop or the loop ends I need to determine which case happened. One method is to create a flag or store variable that will let me do a second test to see how the loop was exited.

For example assume that I need to search through a list and process each item until a flag item is found and then stop processing. If the flag item is missing then an exception needs to be raised.

Using the Python for...else construct you have

for i in mylist:
    if i == theflag:
        break
    process(i)
else:
    raise ValueError("List argument missing terminal flag.")

Compare this to a method that does not use this syntactic sugar:

flagfound = False
for i in mylist:
    if i == theflag:
        flagfound = True
        break
    process(i)

if not flagfound:
    raise ValueError("List argument missing terminal flag.")

In the first case the raise is bound tightly to the for loop it works with. In the second the binding is not as strong and errors may be introduced during maintenance.

Healall answered 2/4, 2012 at 17:13 Comment(11)
I'd have to say this syntactic sugar might rot your project's teeth. This would not make a Python: the good parts book.Hassan
Can you confirm that in your example, process(i) happens for every item in mylist strictly before theflag, and not to theflag itself? Is it what was intended?Cathcart
process will execute on each i that exists in the list before theflag is reached, it will not be executed on elements in the list after theflag, and it will not be executed on theflag.Healall
I think it is useful in case of inner loops. For example, inner loop wants to break the outer loop on break statement.Sequacious
I like this response. The only suggestion I would make is the logic afterwards to more closely resemble the 'else' in the loop. For example: >>if flagfound: >> # do nothing; the flag was found >> pass >>else: >> # same code as the for-else >> raise ValueError("List argument missing terminal flag.")Staples
the else statement also gets executed if the iterable has no elementsGlean
I think they made a mistake by calling it else:. It's really misleading. Before trying it, I was pretty sure it runs when there are no elements to iterate. More accurate name would have been passed:, completed: or something.Bound
As for maintenance, put the "for-else" into an "if" block and see if your maintainers don't more than once out-dent the "else" to bind to the "if" thinking they are fixing a bug.Definiendum
Also, without for/else, you get lint warnings ("might be referenced before assignment") for variables defined inside the loop and used after the loop. Unless you define them before the loop, too.Carnelian
This doesn't answer the question at all. It looks like you didn't read the question, only the title.Hercules
I haven't really seen this used much with extra process(i), only with pure "found or not found" searches. The coupling of "process until this point" condition with "want to know if you stopped" feels somewhat coincidental; if I need both I'd consider a boolean flag which leaves flexibility e.g. keep processing anyway. Or better yet first pure loop to find the index / handle not found case; then separate loop processing slices of the list e.g. mylist[:index].Switchblade
V
403

It's a strange construct even to seasoned Python coders. When used in conjunction with for-loops it basically means "find some item in the iterable, else if none was found do ...". As in:

found_obj = None
for obj in objects:
    if obj.key == search_key:
        found_obj = obj
        break
else:
    print('No object found.')

But anytime you see this construct, a better alternative is to either encapsulate the search in a function:

def find_obj(search_key):
    for obj in objects:
        if obj.key == search_key:
            return obj

Or use a list comprehension:

matching_objs = [o for o in objects if o.key == search_key]
if matching_objs:
    print('Found {}'.format(matching_objs[0]))
else:
    print('No object found.')

It is not semantically equivalent to the other two versions, but works good enough in non-performance critical code where it doesn't matter whether you iterate the whole list or not. Others may disagree, but I personally would avoid ever using the for-else or while-else blocks in production code.

See also [Python-ideas] Summary of for...else threads

Vanzandt answered 2/4, 2012 at 16:30 Comment(15)
The list comprehension is the wrong one-liner. If you're looking for a single item, as in the for loop examples, and want to use a generator expression / list comprehension, then you want next((o for o in objects if o.key == search_key), None) or wrap it in a try / except and use no default value instead of an if / else.Ouachita
and like Lance Helsten's answer, there are actual cases where it's better to use a for/else construct.Obel
Cheers. I had a badly indented file where an else got paired with a for and I had no idea that was legal.Psycholinguistics
The for...else construct is useful at least for the cases where you want to implement get_or_create() type of operations, i.e. looping through a data structure and get the first match if none was found then create a new entry in the data structure.Stepchild
I think that the for loop is the most obvious of the constructs there.Pernik
It is worth mentioning that the else clause will run even if the for loop has values unless a break statement is explicity run as in this example. From the docs above: "The else clause has another perceived problem: if there is no break in the loop, the else clause is functionally redundant.". e.g. for x in [1, 2, 3]:\n print x\n else:\n print 'this executes due to no break'V
In the list-comprehension, there is no break !Sequacious
it's strange if you don't think of the case but i don't find it to be that strange. the case for not having something to loop through is common. semantically for beginners or non-programmers, though, it's probably strange.Who
For the function version, should there be print('No object found.') as the last line of the function?Neglect
I'm happy that I didn't read "find some item in the iterable, else if none was found do ..." before actually knowing how for-else works because this would horribly mislead me how for-else works. else executes when loop ends naturally (i.e. w/o break) and the "find" part is false for regular for-else loop w/o if and break statements which you added in your code.Ikkela
""find some item in the iterable, else if none was found do ..." this is wrong. We iterate for lots of reasons other than "finding things".Rating
Am I right in thinking that the else block of a for-loop only executes if the for-loop completes running THROUGH THE WHOLE TARGET ITERABLE? I assume this syntax is extremely suited for the case of a for loop that terminates or breaks once a target is found. Assuming the loop is coded to break when a target is found, it is logical that when the loop completes without being interrupted, no item was found and the search condition is False.Tabriz
I agree with several previous comments that this answer is misleading and/or partially incorrect. The answer by @lance-helsten is spot on and should be the accepted answer.Demivolt
@Rating No, it is not wrong. The for-else idiom always implements a search with a fallback in case the search fails. For loops are indeed used for lots of reasons, but that is not what this question or answer is about.Vanzandt
If it's only necessary to determine whether there is a match, rather than actually reporting the match, using any is simpler than the search function. In 3.8 and above, any can also report the first match using the walrus operator (:=).Georgetown
S
268

There's an excellent presentation by Raymond Hettinger, titled Transforming Code into Beautiful, Idiomatic Python, in which he briefly addresses the history of the for ... else construct. The relevant section is "Distinguishing multiple exit points in loops" starting at 15:50 and continuing for about three minutes. Here are the high points:

  • The for ... else construct was devised by Donald Knuth as a replacement for certain GOTO use cases;
  • Reusing the else keyword made sense because "it's what Knuth used, and people knew, at that time, all [for statements] had embedded an if and GOTO underneath, and they expected the else;"
  • In hindsight, it should have been called "no break" (or possibly "nobreak"), and then it wouldn't be confusing.*

So, if the question is, "Why don't they change this keyword?" then Cat Plus Plus probably gave the most accurate answer – at this point, it would be too destructive to existing code to be practical. But if the question you're really asking is why else was reused in the first place, well, apparently it seemed like a good idea at the time.

Personally, I like the compromise of commenting # no break in-line wherever the else could be mistaken, at a glance, as belonging inside the loop. It's reasonably clear and concise. This option gets a brief mention in the summary that Bjorn linked at the end of his answer:

For completeness, I should mention that with a slight change in syntax, programmers who want this syntax can have it right now:

for item in sequence:
    process(item)
else:  # no break
    suite

* Bonus quote from that part of the video: "Just like if we called lambda makefunction, nobody would ask, 'What does lambda do?'"

Sightless answered 19/5, 2014 at 22:30 Comment(10)
Why not add support for nobreak next to else, have both be equal and exist alongside eachother and make a clear PEP style rule that nobreak should be used instead of else?Chimerical
@Chimerical I can't speak for Python core devs but consider the PEP 20 line "There should be one-- and preferably only one --obvious way to do it."Sightless
yeah that's true, but they did the same thing with the division operator, where one could import division from __future__ to replace / with standard division and add the // floor division operator.Chimerical
recent pep doc states that multiple ways is OK, just not multiple obvious ways. Since a nobreak keyword could be more obvious, maybe this is indeed a way to improve the syntax.Chimerical
"nobreak" would be a new keyword, and, as a rule, language designers are very hesitant to add keywords to an existing language, because it insta-breaks all code that has used that identifier for something else.Protolanguage
Though the example is incomplete because for ... else never makes sense without break.Importunacy
The # no break comment seems like a really nice idea to me. Only seeing else can be very misleading, as in 99% of cases else is related to an if, so adding a comment like that can be useful. That's what comments should be used for, after all - commenting non-obvious or potentially misleading stuff.Dyane
@Protolanguage The new PEG grammar makes it easier to introduce new "context-sensitive" keywords (Python match remains a valid identifier in 3.10 even though it also introduces the new pattern-matching statement.) However, I do think the bar should still remain high for adding new keywords.Galactometer
Thanks for the actual answer to the question. My take is that it's a symptom of the PHP-disease. Like PHP just included stuff from everywhere, and left the source naming conventions intact; camelCase, PascalCase, snake_case, whatever the source of some functions used, PHP used. Now I realize that Python, or rather Guido, wasn't immune to this. "We do for... else because Knuth did it". Same for continue. Both bad non-choices. And don't get me started on the return value of os.stat and the stat module.Cryobiology
@Protolanguage Which means it was exactly the thing Python 3000 was meant to do (and did, in many cases).Cryobiology
A
70

To make it simple, you can think of it like that;

  • If it encounters the break command in the for loop, the else part will not be called.
  • If it does not encounter the break command in the for loop, the else part will be called.

In other words, if for loop iteration is not "broken" with break, the else part will be called.

Andreeandrei answered 27/10, 2017 at 11:3 Comment(4)
The else block will also not be executed if the body of the loop raises an exception.Bearer
And the else block will also be executed if the list is empty and the for loop does not iterate at all.Demivolt
If like to read for/else as for/nobreak.Transsonic
@Amal K the else still executes if the exception is handled with a try-exceptFundus
S
47

Because they didn't want to introduce a new keyword to the language. Each one steals an identifier and causes backwards compatibility problems, so it's usually a last resort.

Stupendous answered 2/4, 2012 at 16:20 Comment(9)
Seems like finally would have been a better choice in that case. Was the finally keyword not yet present at the time this construct was introduced?Uptodate
@Wallacoloo finally isn't much better, because it implies the block would always be executed after the loop, and it isn't (because that'd be redundant with just putting the code to run after the loop).Stupendous
It also cannot be finally because the else clause is executed also when continue is used in the for loop -- that is possibly many times and not only at the end.Bret
@Bret else clause execution is not affected by continue (docs and test code)Sightless
@AirThomas: +1. You are right. The else is executed only when the continue was the one for the last iteration.Bret
completed would have been a better keyword here; to signify the that the for-loop's execution was not broken (irrespective of whether any loops were actually performed). At least then if someone accidentally shifts the indentation it wouldn't end up causing incorrect behaviour because the else has suddenly moved to an if construct....Leadership
else would have been more fitting if the loop did not run at all (no matching elements)Leadership
the best keyword would be something like afterBreak, but ofcourse a keyword should be one singular word, so not really sure what the keyword should be.Gavage
@Gavage replacing else with afterBreak doesn't make sense. else doesn't execute after a break. else executes if the loop finishes without the use of a break or uncaught exception.Anett
C
27

The easiest way I found to 'get' what the for/else did, and more importantly, when to use it, was to concentrate on where the break statement jumps to. The For/else construct is a single block. The break jumps out of the block, and so jumps 'over' the else clause. If the contents of the else clause simply followed the for clause, it would never be jumped over, and so the equivalent logic would have to be provided by putting it in an if. This has been said before, but not quite in these words, so it may help somebody else. Try running the following code fragment. I'm wholeheartedly in favour of the 'no break' comment for clarity.

for a in range(3):
    print(a)
    if a==4: # change value to force break or not
        break
else: #no break  +10 for whoever thought of this decoration
    print('for completed OK')

print('statement after for loop')

EDIT - I notice this question is still running

Second better thoughts ...

The 'no break' comment is a negative. It's so much easier to understand a positive assertion, and that is that the for iterable was exhausted.

for a in range(3):
    print(a)
    if a==4: # change value to force break or not
        print('ending for loop with a break')
        break
else: # when iterable exhausted  
    print('ending for loop as iterable exhausted')

print('for loop ended one way or another')

That also reinforces this interpretation

if iterable_supplies_a_value:
    run_the_for_with_that_value
else:
    do_something_else
Cockleshell answered 5/2, 2015 at 12:54 Comment(2)
"The break jumps out of the block, and so jumps 'over' the else clause" - while this may be helpful as a way of "getting" for:/else:, it doesn't really provide a justification for the keyword being else. Given the framing given here, then: seems like it would be much more natural. (There are reasons for else being chosen, given in other answers - they're just not provided here.)Posy
@Cockleshell "if iterable_supplies_a_value:" sounds like "else:" runs only when the iterable was empty, which is a common wrong guess of what it does. But previous parts of your answer explain it correctly. As written, it's unclear what you meant by "That also reinforces this interpretation"Switchblade
M
24

I think documentation has a great explanation of else, continue

[...] it is executed when the loop terminates through exhaustion of the list (with for) or when the condition becomes false (with while), but not when the loop is terminated by a break statement."

Source: Python 2 docs: Tutorial on control flow

Medial answered 13/5, 2014 at 8:28 Comment(0)
D
18

Since the technical part has been pretty much answered, my comment is just in relation with the confusion that produce this recycled keyword.

Being Python a very eloquent programming language, the misuse of a keyword is more notorious. The else keyword perfectly describes part of the flow of a decision tree, "if you can't do this, (else) do that". It's implied in our own language.

Instead, using this keyword with while and for statements creates confusion. The reason, our career as programmers has taught us that the else statement resides within a decision tree; its logical scope, a wrapper that conditionally return a path to follow. Meanwhile, loop statements have a figurative explicit goal to reach something. The goal is met after continuous iterations of a process.

if / else indicate a path to follow. Loops follow a path until the "goal" is completed.

The issue is that else is a word that clearly define the last option in a condition. The semantics of the word are both shared by Python and Human Language. But the else word in Human Language is never used to indicate the actions someone or something will take after something is completed. It will be used if, in the process of completing it, an issue rises (more like a break statement).

At the end, the keyword will remain in Python. It's clear it was mistake, clearer when every programmer tries to come up with a story to understand its usage like some mnemonic device. I'd have loved if they have chosen instead the keyword then. I believe that this keyword fits perfectly in that iterative flow, the payoff after the loop.

It resembles that situation that some child has after following every step in assembling a toy: And THEN what Dad?

Depict answered 12/1, 2016 at 18:12 Comment(2)
I think that this answer addresses the issue of confusion I think the OP was talking about. The else keyword does exactly the opposite of what you would expect of from the English meaning of else when attached to the action of for. In theory, the for ... else could have worked differently in that you end up in the else part when the loop is broken out of, But the problem is that to use it to find element x, and handle the case where x is not found, you may have to use a flag or another test after the whole for .. else constructStoops
My career as a programmer has taught me nothing of the sort. Even if I had had a career as a programmer, this would still be true.Cryobiology
I
15

Great answers are:

  • this which explain the history, and
  • this gives the right citation to ease yours translation/understanding.

My note here comes from what Donald Knuth once said (sorry can't find reference) that there is a construct where while-else is indistinguishable from if-else, namely (in Python):

x = 2
while x > 3:
    print("foo")
    break
else:
    print("boo")

has the same flow (excluding low level differences) as:

x = 2
if x > 3:
    print("foo")
else:
    print("boo")

The point is that if-else can be considered as syntactic sugar for while-else which has implicit break at the end of its if block. The opposite implication, that while loop is extension to if, is more common (it's just repeated/looped conditional check), because if is often taught before while. However that isn't true because that would mean else block in while-else would be executed each time when condition is false.

To ease your understanding think of it that way:

Without break, return, etc., loop ends only when condition is no longer true and in such case else block will also execute once. In case of Python for you must consider C-style for loops (with conditions) or translate them to while.

Another note:

Premature break, return, etc. inside loop makes impossible for condition to become false because execution jumped out of the loop while condition was true and it would never come back to check it again.

Ikkela answered 21/2, 2019 at 16:57 Comment(0)
S
14

I read it something like:

If still on the conditions to run the loop, do stuff, else do something else.

Shrapnel answered 2/4, 2012 at 16:20 Comment(2)
Your still on the conditions is helpful (+1) although it is wrong - it's human ;-)Orbit
-1; this pronunciation of for:/else: makes it sound like the else: will always run after the loop, which isn't the case.Posy
E
10

I'm wondering how Python coders read this construct in their head (or aloud, if you like).

I simply think in my head:

"else no break was encountered..."

That's it!

This is because the else clause executes only if a break statement was NOT encountered in the for loop.

Reference:

See here: https://book.pythontips.com/en/latest/for_-_else.html#else-clause (emphasis added, and "not" changed to "NOT"):

for loops also have an else clause which most of us are unfamiliar with. The else clause executes after the loop completes normally. This means that the loop did NOT encounter a break statement.


That being said, I recommend against using this unusual feature of the language. Don't use the else clause after a for loop. It's confusing to most people, and just slows down their ability to read and understand the code.

Eloyelreath answered 24/3, 2021 at 5:39 Comment(1)
I could not convince myself that normal completion of a loop is through a break statement. For me it is otherwise. hence not using this weird overthought construct.Buff
C
7

I read it like "When the iterable is exhausted completely, and the execution is about to proceed to the next statement after finishing the for, the else clause will be executed." Thus, when the iteration is broken by break, this will not be executed.

Chutzpah answered 7/7, 2014 at 4:32 Comment(0)
H
6

I agree, it's more like an 'elif not [condition(s) raising break]'.

I know this is an old thread, but I am looking into the same question right now, and I'm not sure anyone has captured the answer to this question in the way I understand it.

For me, there are three ways of "reading" the else in For... else or While... else statements, all of which are equivalent, are:

  1. else == if the loop completes normally (without a break or error)
  2. else == if the loop does not encounter a break
  3. else == else not (condition raising break) (presumably there is such a condition, or you wouldn't have a loop)

So, essentially, the "else" in a loop is really an "elif ..." where '...' is (1) no break, which is equivalent to (2) NOT [condition(s) raising break].

I think the key is that the else is pointless without the 'break', so a for...else includes:

for:
    do stuff
    conditional break # implied by else
else not break:
    do more stuff

So, essential elements of a for...else loop are as follows, and you would read them in plainer English as:

for:
    do stuff
    condition:
        break
else: # read as "else not break" or "else not condition"
    do more stuff

As the other posters have said, a break is generally raised when you are able to locate what your loop is looking for, so the else: becomes "what to do if target item not located".

Example

You can also use exception handling, breaks, and for loops all together.

for x in range(0,3):
    print("x: {}".format(x))
    if x == 2:
        try:
            raise AssertionError("ASSERTION ERROR: x is {}".format(x))
        except:
            print(AssertionError("ASSERTION ERROR: x is {}".format(x)))
            break
else:
    print("X loop complete without error")

Result

x: 0
x: 1
x: 2
ASSERTION ERROR: x is 2
----------
# loop not completed (hit break), so else didn't run

Example

Simple example with a break being hit.

for y in range(0,3):
    print("y: {}".format(y))
    if y == 2: # will be executed
        print("BREAK: y is {}\n----------".format(y))
        break
else: # not executed because break is hit
    print("y_loop completed without break----------\n")

Result

y: 0
y: 1
y: 2
BREAK: y is 2
----------
# loop not completed (hit break), so else didn't run

Example

Simple example where there no break, no condition raising a break, and no error are encountered.

for z in range(0,3):
     print("z: {}".format(z))
     if z == 4: # will not be executed
         print("BREAK: z is {}\n".format(y))
         break
     if z == 4: # will not be executed
         raise AssertionError("ASSERTION ERROR: x is {}".format(x))
else:
     print("z_loop complete without break or error\n----------\n")

Result

z: 0
z: 1
z: 2
z_loop complete without break or error
----------
Hadhramaut answered 6/1, 2016 at 17:40 Comment(0)
S
6

The else keyword can be confusing here, and as many people have pointed out, something like nobreak, notbreak is more appropriate.

In order to understand for ... else ... logically, compare it with try...except...else, not if...else..., most of python programmers are familiar with the following code:

try:
    do_something()
except:
    print("Error happened.") # The try block threw an exception
else:
    print("Everything is find.") # The try block does things just find.

Similarly, think of break as a special kind of Exception:

for x in iterable:
    do_something(x)
except break:
    pass # Implied by Python's loop semantics
else:
    print('no break encountered')  # No break statement was encountered

The difference is python implies except break and you can not write it out, so it becomes:

for x in iterable:
    do_something(x)
else:
    print('no break encountered')  # No break statement was encountered

Yes, I know this comparison can be difficult and tiresome, but it does clarify the confusion.

Shinar answered 27/4, 2017 at 3:36 Comment(5)
You should make a link to resource when you copy from it: Nick Coghlan's Python Notes.Mortgagee
@Mortgagee thanks for the link. I read and accept the concept when first learning python, didn't memorize the source when writing the answer.Shinar
@Shinar You "didn't memorise the source" but just happened to include entire sentences of comments identical to the original? Ooookaaaay.Posy
I came here looking for this one, but.. isn't try: stuff(); except: error(); else: ok() really the same as try: stuff(); ok(); except: error() ?Thrombophlebitis
@phil Not exactly, because if there's an exception raised inside else, it'll bubble up.Cestar
D
5

Codes in else statement block will be executed when the for loop was not be broke.

for x in xrange(1,5):
    if x == 5:
        print 'find 5'
        break
else:
    print 'can not find 5!'
#can not find 5!

From the docs: break and continue Statements, and else Clauses on Loops

Loop statements may have an else clause; it is executed when the loop terminates through exhaustion of the list (with for) or when the condition becomes false (with while), but not when the loop is terminated by a break statement. This is exemplified by the following loop, which searches for prime numbers:

>>> for n in range(2, 10):
...     for x in range(2, n):
...         if n % x == 0:
...             print(n, 'equals', x, '*', n//x)
...             break
...     else:
...         # loop fell through without finding a factor
...         print(n, 'is a prime number')
...
2 is a prime number
3 is a prime number
4 equals 2 * 2
5 is a prime number
6 equals 2 * 3
7 is a prime number
8 equals 2 * 4
9 equals 3 * 3

(Yes, this is the correct code. Look closely: the else clause belongs to the for loop, not the if statement.)

When used with a loop, the else clause has more in common with the else clause of a try statement than it does that of if statements: a try statement’s else clause runs when no exception occurs, and a loop’s else clause runs when no break occurs. For more on the try statement and exceptions, see Handling Exceptions.

The continue statement, also borrowed from C, continues with the next iteration of the loop:

>>> for num in range(2, 10):
...     if num % 2 == 0:
...         print("Found an even number", num)
...         continue
...     print("Found a number", num)
Found an even number 2
Found a number 3
Found an even number 4
Found a number 5
Found an even number 6
Found a number 7
Found an even number 8
Found a number 9
Dahlia answered 26/8, 2016 at 4:42 Comment(1)
This adds nothing and doesn't answer the question, which is not how but why.Sightless
W
5

Here's a way to think about it that I haven't seen anyone else mention above:

First, remember that for-loops are basically just syntactic sugar around while-loops. For example, the loop

for item in sequence:
    do_something(item)

can be rewritten (approximately) as

item = None
while sequence.hasnext():
    item = sequence.next()
    do_something(item)

Second, remember that while-loops are basically just repeated if-blocks! You can always read a while-loop as "if this condition is true, execute the body, then come back and check again".

So while/else makes perfect sense: It's the exact same structure as if/else, with the added functionality of looping until the condition becomes false instead of just checking the condition once.

And then for/else makes perfect sense too: because all for-loops are just syntactic sugar on top of while-loops, you just need to figure out what the underlying while-loop's implicit conditional is, and then the else corresponds to when that condition becomes False.

Woehick answered 11/12, 2018 at 17:52 Comment(0)
A
3
for i in range(3):
    print(i)

    if i == 2:
        print("Too big - I'm giving up!")
        break;
else:
    print("Completed successfully")

"else" here is crazily simple, just mean

1, "if for clause is completed"

for i in range(3):
    print(i)

    if i == 2:
        print("Too big - I'm giving up!")
        break;
if "for clause is completed":
    print("Completed successfully")

It's wielding to write such long statements as "for clause is completed", so they introduce "else".

else here is a if in its nature.

2, However, How about for clause is not run at all

In [331]: for i in range(0):
     ...:     print(i)
     ...: 
     ...:     if i == 9:
     ...:         print("Too big - I'm giving up!")
     ...:         break
     ...: else:
     ...:     print("Completed successfully")
     ...:     
Completed successfully

So it's completely statement is logic combination:

if "for clause is completed" or "not run at all":
     do else stuff

or put it this way:

if "for clause is not partially run":
    do else stuff

or this way:

if "for clause not encounter a break":
    do else stuff
Acacia answered 25/8, 2018 at 4:29 Comment(1)
else acts as "transaction" in SQL.Acacia
H
2

Here's another idiomatic use case besides searching. Let's say you wanted to wait for a condition to be true, e.g. a port to be open on a remote server, along with some timeout. Then you could utilize a while...else construct like so:

import socket
import time

sock = socket.socket()
timeout = time.time() + 15
while time.time() < timeout:
    if sock.connect_ex(('127.0.0.1', 80)) is 0:
        print('Port is open now!')
        break
    print('Still waiting...')
else:
    raise TimeoutError()
Hawken answered 2/3, 2018 at 14:48 Comment(0)
B
2

I was just trying to make sense of it again myself. I found that the following helps!

• Think of the else as being paired with the if inside the loop (instead of with the for) - if condition is met then break the loop, else do this - except it's one else paired with multiple ifs!
• If no ifs were satisfied at all, then do the else.
• The multiple ifs can also actually be thought of as if-elifs!

Bolinger answered 11/12, 2018 at 15:6 Comment(1)
there is no need for an if in the loop, and there's no need for a loop either - you can use else with try-except, for instanceThrombophlebitis
F
2

A loop's else branch executes once, regardless of whether the loop enters its body or not, unless the loop body is entered but does not finish. That is, inside the loop a break or return statement is encountered.

my_list = []
for i in my_list:
    print(i, end=',')
else:
    print('loop did not enter')

##################################    

for i in range(1,6,1):
    print(i, end=',')
else:
    print('loop completed successfully:', i)

##################################    

for i in range(1,6,1):
    if i == 3:
        print('loop did not finish:', i)
        break
    print(i, end=',')
else:
    print('else:', i)

Output:

loop did not enter
1,2,3,4,5,loop completed successfully: 5
1,2,loop did not finish: 3

It's the same for while-else.

import random
random.seed(8)

i = 100
while i < 90:
    print(i, end=',')
    i = random.randint(0,100)
else:
    print('loop did not enter:', i)

##################################    

i = 25
while i < 90:
    print(i, end=',')
    i = random.randint(0,100)
else:
    print('loop completed successfully:', i)

##################################    

i = 25
while i < 90:
    if i % 10 == 0:
        print('loop did not finish:', i)
        break
    print(i, end=',')
    i = random.randint(0,100)
else:
    print('else:', i)

Output:

loop did not enter: 100
25,29,47,48,16,24,loop completed successfully: 90
25,5,loop did not finish: 10
Fundus answered 11/11, 2022 at 5:40 Comment(0)
M
2

It may seems confusing for programmers as we always expect to see if when we face an else, but in real life we use else with for

As an example, I ask my son to search the bookshelf for dictionary and bring it to me, and if he can not find it then he should go and buy one for me

This is the algorithm in his mind

for book in bookshelf:
    if book == 'dictionary':
        #Lucky Me !! I find it lets bring it to dad
        bring('dictionary')
        break #and of course I break and stop searching 
else: #too bad :( we do not have any dictionaries
    buy('dictionary');
Milzie answered 26/7, 2023 at 9:22 Comment(0)
B
1
for i in range(10):
    print(i)

    if i == 9:
        print("Too big - I'm giving up!")
        break;
else:
    print("Completed successfully")

break keyword is used to end the loop. if the i = 9 then the loop will end. while any if conditions did not much the satisfaction, then the else will do the rest part.

Braces answered 16/3, 2021 at 10:57 Comment(0)
M
1

The else clause executes after the loop completes normally. This means The :==> else block just after for/while is executed only when the loop is NOT terminated by a break statement

for item in lista:
if(obj == item ):
    print("if True then break will run and else not run")
    break;
else:
print("in  else => obj not fount ")
Mineral answered 5/9, 2021 at 17:24 Comment(0)
C
0

You could think of it like, else as in the rest of the stuff, or the other stuff, that wasn't done in the loop.

Caracalla answered 2/4, 2012 at 16:25 Comment(0)
C
-2

I consider the structure as for (if) A else B, and for(if)-else is a special if-else, roughly. It may help to understand else.

A and B is executed at most once, which is the same as if-else structure.

for(if) can be considered as a special if, which does a loop to try to meet the if condition. Once the if condition is met, A and break; Else, B.

Chufa answered 13/2, 2019 at 18:41 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.