mutating a deque whilst iterating over it

duncan smith duncan at invalid.invalid
Thu Feb 11 15:22:11 EST 2021


Hello,
      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? It seems a bit inconsistent. Cheers.

Duncan

>>> from collections import deque
>>> d = deque(range(8))
>>> it = iter(d)
>>> next(it)
0
>>> d[1] = 78
>>> next(it)
78
>>> d.append(8)
>>> next(it)
Traceback (most recent call last):
  File "<pyshell#650>", line 1, in <module>
    next(it)
RuntimeError: deque mutated during iteration
>>>


More information about the Python-list mailing list