Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python3 StopIteration Error can not raise when using list(map(...))

At first I write this, and it raises StopIteration, works well.

it = iter([1])
iters = [it] * 2
for it in iters:
    r = next(it)
    print(r)

but when I changed to this:

it = iter([1])
iters = [it] * 2
r = list(map(next, iters))
print(r)

It can not raises StopIteration, How to explain this?

like image 329
user1021531 Avatar asked Aug 26 '26 05:08

user1021531


1 Answers

list constructor detects that the argument is an iterable, so it iterates on it a bit like this (I'm making this up):

def __init__(self,iterable):
    # some init stuff to create the list
    while True:
       try:
           next_element = next(iterable)
           self.append(next_element)
       except StopIteration:
           break

At some point the iterable raises StopIteration, so list intercepts it and ends the list (it has no other way to do so with generators, the exception is the signal that indicates that the iteration has ended)

Same as:

list(map(next,iter([])))

here, the exception is propagated from a level below. map is not raising it, but next is. For list constructor, which is catching the StopIteration exception, it doesn't make a difference which level raises it.

And yes, there's no way you can let StopIteration "escape" from the list constructor, but you can get this raised with a list comprehension:

r = [next(it) for it in iters]

(since iters is the one getting watched for StopIteration, not map, so you have found a case where list(map(f,y)) isn't the same as [f(x) for x in y])

like image 195
Jean-François Fabre Avatar answered Aug 29 '26 08:08

Jean-François Fabre



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!