The Python `StopIteration` exception.
Questions tagged [stopiteration]
72 questions
99
votes
1 answer
Why does next raise a 'StopIteration', but 'for' do a normal return?
In this piece of code, why does using for result in no StopIteration
or is the for loop trapping all exceptions and then silently exiting?
In which case, why do we have the extraneous return?? Or is the
raise StopIteration caused by: return…
user621819
81
votes
7 answers
"RuntimeError: generator raised StopIteration" every time I try to run app
I am trying to run this code in Python 3.7:
import web
urls = ('/', 'index')
if __name__ == "__main__":
app = web.application(urls, globals())
app.run()
But it gives me this error everytime:
C:\Users\aidke\Desktop>python app.py
Traceback…

no4syn
- 813
- 1
- 6
- 4
77
votes
2 answers
How can I get a Python generator to return None rather than StopIteration?
I am using generators to perform searches in lists like this simple example:
>>> a = [1,2,3,4]
>>> (i for i, v in enumerate(a) if v == 4).next()
3
(Just to frame the example a bit, I am using very much longer lists compared to the one above, and…

c00kiemonster
- 22,241
- 34
- 95
- 133
59
votes
3 answers
What is the difference between raise StopIteration and a return statement in generators?
I'm curious about the difference between using raise StopIteration and a return statement in generators.
For example, is there any difference between these two functions?
def my_generator0(n):
for i in range(n):
yield i
if i >=…

slallum
- 2,161
- 4
- 24
- 24
45
votes
5 answers
How yield catches StopIteration exception?
Why in the example function terminates:
def func(iterable):
while True:
val = next(iterable)
yield val
but if I take off yield statement function will raise StopIteration exception?
EDIT: Sorry for misleading you guys. I know…

Sergey Ivanov
- 3,719
- 7
- 34
- 59
23
votes
3 answers
python yield and stopiteration in one loop?
i have a generator where i would like to add an initial and final value to the actual content, it's something like this:
# any generic queue where i would like to get something from
q = Queue()
def gen( header='something', footer='anything' ):
…

yee379
- 6,498
- 10
- 56
- 101
17
votes
1 answer
How to avoid StopIteration Error in python
I have a line that is pulling in variables from multiple lists and I want it to avoid the StopIteration error that comes up so that it can move onto the next line. At the moment I am using the break function, this avoids the StopIteration, but only…

Johnnerz
- 1,365
- 4
- 17
- 29
15
votes
1 answer
Why am I getting a StopIteration error when using next()?
I am not able to clarify my self over the use of next() in python(3).
I have a data :
chr pos ms01e_PI ms01e_PG_al ms02g_PI ms02g_PG_al ms03g_PI ms03g_PG_al ms04h_PI ms04h_PG_al
2 15881989 4 C|C 6 A|C 7 C|C 7 C|C
2 …

everestial007
- 6,665
- 7
- 32
- 72
11
votes
1 answer
Python PEP479 Change StopIteration handling inside generators
Could someone help me understand what PEP479 is about? I was reading the doc and couldn't get my head around it.
The abstract says:
This PEP proposes a change to generators: when StopIteration is raised inside a generator, it is replaced it with…

Jacques Gaudin
- 15,779
- 10
- 54
- 75
11
votes
3 answers
Python: StopIteration exception and list comprehensions
I'd like to read at most 20 lines from a csv file:
rows = [csvreader.next() for i in range(20)]
Works fine if the file has 20 or more rows, fails with a StopIteration exception otherwise.
Is there an elegant way to deal with an iterator that could…

Parand
- 102,950
- 48
- 151
- 186
10
votes
4 answers
Return from an iterator and then throw StopIteration
What would be the nice way to return something from an iterator one last time when it's exhausted. I'm using a flag, but this is rather ugly:
class Example():
def __iter__(self):
self.lst = [1,2,3]
self.stop = False # <-- ugly …

georg
- 211,518
- 52
- 313
- 390
10
votes
1 answer
When will a StopIteration be converted into RuntimeError?
I'm reading the documentation for Python 3 here:
If a generator code directly or indirectly raises StopIteration, it is converted into a RuntimeError (retaining the StopIteration as the new exception's cause).
I don't understand that, can anyone…

iBug
- 35,554
- 7
- 89
- 134
8
votes
4 answers
Sending StopIteration to for loop from outside of the iterator
There are several ways to break out of a few nested loops
They are:
1) to use break-continue
for x in xrange(10):
for y in xrange(10):
print x*y
if x*y > 50:
break
else:
continue # only executed if break…

ovgolovin
- 13,063
- 6
- 47
- 78
7
votes
1 answer
Iterate through list and handle StopIteration in Python beautifully
I am trying to iterate through a list, and I need to perform specific operation when and only when the iteration reached the end of the list, see example below:
data = [1, 2, 3]
data_iter = data.__iter__()
try:
while True:
item =…

Serge Tarkovski
- 1,861
- 6
- 19
- 24
3
votes
1 answer
Understanding StopIteration handling inside generators for non-trivial case
I am helping maintain some code that now includes automated Python 3.7 testing. This led me to some issues related to PEP 479 "Change StopIteration handling inside generators". My naive understanding was that you could use a try-except block to…

Chris_Rands
- 38,994
- 14
- 83
- 119