for-else expression
The for statement is used to iterate over the elements of a sequence (such as a string, tuple or list) or other iterable object:
for_stmt ::= “for” target_list “in” expression_list “:” suite
[“else” “:” suite]
is seems to
it = iter(expression_list)
while True:
try:
target_list = next(it)
suite
except StopIteration:
break
The expression list is evaluated once; it should yield an iterable object. An iterator is created for the result of the
expression_list
. The suite is then executed once for each item provided by the iterator, in the order returned by the iterator. Each item in turn is assigned to the target list using the standard rules for assignments (see Assignment statements), and then the suite is executed. When the items are exhausted (which is immediately when the sequence is empty or an iterator raises aStopIteration
exception), the suite in theelse
clause, if present, is executed, and the loop terminates.A
break
statement executed in the first suite terminates the loop without executing theelse
clause’s suite. Acontinue
statement executed in the first suite skips the rest of the suite and continues with thenext
item, or with theelse
clause if there is no next item.for-else example
def find_item(list, n):
for item in list:
if n == item:
print('I found it!')
break
else:
print("I can't find it!")
>>> find_item(range(10), 11)
I can't find it!
>>>
>>> find_item(range(10), 7)
I found it!
- The for-loop makes assignments to the variables(s) in the target list. This overwrites all previous assignments to those variables including those made in the suite of the for-loop:
>>> for i in range(10):
... print(i)
... i = 5
...
0
1
2
3
4
5
6
7
8
9
- There is a subtlety when the sequence is being modified by the loop (this can only occur for mutable sequences, i.e. lists). An internal counter is used to keep track of which item is used next, and this is incremented on each iteration. When this counter has reached the length of the sequence the loop terminates. This means that if the suite deletes the current (or a previous) item from the sequence, the next item will be skipped (since it gets the index of the current item which has already been treated). Likewise, if the suite inserts an item in the sequence before the current item, the current item will be treated again the next time through the loop. This can lead to nasty bugs that can be avoided by making a temporary copy using a slice of the whole sequence, e.g
>>> a
[-5, -4, -3, -2, -1, 0, 1, 2, 3, 4]
>>> for x in a:
... if x<0:
... a.remove(x)
...
>>> a
[-4, -2, 0, 1, 2, 3, 4]
>>> a
[-5, -4, -3, -2, -1, 0, 1, 2, 3, 4]
>>> for x in a[:]:
... if x < 0: a.remove(x)
...
>>> a
[0, 1, 2, 3, 4]
user's for-loop
class MyIter:
def __iter__(self):
return self
def next(self):
if not condition:
raise StopIteration
value = calculate next value
return value
for item in MyIter():
do something with item