mutating a deque whilst iterating over it

Terry Reedy tjreedy at udel.edu
Thu Feb 11 22:04:37 EST 2021


On 2/11/2021 3:22 PM, duncan smith wrote:

>        It seems that I can mutate a deque while iterating over it if I
> assign to an index, but not if I append to it. Is this the intended
> behaviour?

Does the deque doc say anything about mutation while iterating? 
(Knowing the author of deque, I would consider everything about it 
intentional without *good* reason to think otherwise.

>>>> from collections import deque
>>>> d = deque(range(8))
>>>> it = iter(d)
>>>> next(it)
> 0
>>>> d[1] = 78

This does not change the structure of the deque, so next does not 
notice.  It could be considered not be a mutation.  It could be detected 
by changing deque.__setitem__, but why bother and slow down all 
__setitem__ calls.

>>>> next(it)
> 78
>>>> d.append(8)

This changes the structure, which can apparently mess-up iteration.

>>>> next(it)
> Traceback (most recent call last):
>    File "<pyshell#650>", line 1, in <module>
>      next(it)
> RuntimeError: deque mutated during iteration
>>>>


-- 
Terry Jan Reedy



More information about the Python-list mailing list