On Wed, May 30, 2018 at 10:55 PM, Steven D'Aprano <steve+comp.lang.pyt...@pearwood.info> wrote: > On Wed, 30 May 2018 05:03:02 -0400, Terry Reedy wrote: > >> On 5/30/2018 4:48 AM, Frank Millman wrote: >>> Hi all >>> >>> I want to work backwards to solve this problem, so I have to explain it >>> forwards to put you in the picture. >>> >>> I have an Ordered Dictionary. Under certain circumstances I am getting >>> this error - >>> >>> RuntimeError: OrderedDict mutated during iteration >> >> This should mean that the value associated with a key was sometimes >> changed. > > I don't think so. It should be legal to iterate over a dict and change > the values. At least it works for me: > > import random > from collections import OrderedDict > d = OrderedDict(zip(range(1, 10), "abcdefghi")) > for i in range(10000): # just in case of intermittent errors > for value in d.values(): > d[random.randrange(1, 10)] = random.random() > > > I get no errors. >
You can always change the *values*, but not the *order* of the keys. >>> from collections import OrderedDict >>> d = OrderedDict(zip(range(1, 10), "abcdefghi")) >>> for x in d: ... if x == 5: d.move_to_end(x) ... Traceback (most recent call last): File "<stdin>", line 1, in <module> RuntimeError: OrderedDict mutated during iteration ChrisA -- https://mail.python.org/mailman/listinfo/python-list