Skip to content
Advertisement

python RuntimeError: dictionary changed size during iteration

I have obj like this

JavaScript

It should be expand to

JavaScript

I wrote code below, splite by '.', remove old key, append new key if contains '.', but it said RuntimeError: dictionary changed size during iteration

JavaScript

for k, v in obj.iteritems():

RuntimeError: dictionary changed size during iteration

Advertisement

Answer

Like the message says: you changed the number of entries in obj inside of expandField() while in the middle of looping over this entries in expand.

You might try instead creating a new dictionary of the form you wish, or somehow recording the changes you want to make, and then making them AFTER the loop is done.

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement