I have a deque in Python that I'm iterating over. Sometimes the deque changes while I'm interating which produces a RuntimeError: deque mutated during iteration
.
If this were a Python list instead of a deque, I would just iterate over a copy of the list (via a slice like my_list[:]
, but since slice operations can't be used on deques, I wonder what the most pythonic way of handling this is?
My solution is to import the copy module and then iterate over a copy, like for item in copy(my_deque):
which is fine, but since I searched high and low for this topic I figured I'd post here to ask?