-1

Programming noob here. All the code I have encountered thus far, generally has if, elif, and else statements nested within some kind of loop. However this code that I have stumbled across has an else statement outside of the for loop, with no preceding if statements at the same indentation.

For some context, the code is an excerpt of a prefix trie.

    for c in head.childs:
        if c.value == l and c.reference != ref:
            c.updateRef(ref)
            return c
        elif c.value == l:
            return c

    else:
        n = Node(l, ref)
        head.childs.append(n)
        return n

I tried to test my understanding of the code by changing it to:

    if len(head.childs) == 0:
        n = Node(l, ref)
        head.childs.append(n)
        return n

    for c in head.childs:
        if c.value == l and c.reference != ref:
            c.updateRef(ref)
            return c
        elif c.value == l:
            return c

However, this causes it to crash.

Could someone please help me understand how this else statement can be outside of the for loop without any preceding if statements with the same indent? Thanks

2 Answers2

0

for-else loops.

The else clause only run if the loop finishes naturally meaning that it does not have any break statements in the body. While loop also can take an else clause just like this. Here is an example of for else.

>>> for i in range(1):
...     break
... else:
...     print('Else is only run if the loop finishes naturally, without breaks')
...
>>> for i in range(1):
...     i
... else:
...     print('Hello')
...
0
Hello
>>>
Işık Kaplan
  • 2,815
  • 2
  • 13
  • 28
0

The for-else loop essentially means that when the iterator you are looping on is empty, execute the else case.

From the docs: https://docs.python.org/3/tutorial/controlflow.html#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.

If the iterator is non-empty like below, the else will be executed after the loop is iterated on

li = [1,2,3 ]
for i in li:
    print(i)
else:
    print('empty list')

The output will be

1
2
3
empty list

But if the iterator is empty, the else will be executed right away

li = []
for i in li:
    print(i)
else:
    print('empty list')

The output will be

empty list

But if you break out of the loop, else is not executed

li = [1,2,3]
for i in li:
    break
else:
    print('empty list')

We won't get any output here

And in the other case, when you have an if before and then the for loop, if is evaluated independently and the for loop is run

li = [1,2,3]
if len(li) == 0:
    print('empty list')

for i in li:
    print(i)

This will give

1
2
3

But the following

li = []
if len(li) == 0:
    print('empty list')

for i in li:
    print(i)

Will output

empty list
Devesh Kumar Singh
  • 20,259
  • 5
  • 21
  • 40