I have obj like this
{hello: \'world\', \"foo.0.bar\": v1, \"foo.0.name\": v2, \"foo.1.bar\": v3}
It should be expand to
{
Rewriting this part
def expand(obj):
for k in obj.keys():
expandField(obj, k, v)
to the following
def expand(obj):
keys = obj.keys()
for k in keys:
if k in obj:
expandField(obj, k, v)
shall make it work.
For those experiencing
RuntimeError: dictionary changed size during iteration
also make sure you're not iterating through a defaultdict
when trying to access a non-existent key! I caught myself doing that inside the for
loop, which caused the defaultdict
to create a default value for this key, causing the aforementioned error.
The solution is to convert your defaultdict
to dict
before looping through it, i.e.
d = defaultdict(int)
d_new = dict(d)
or make sure you're not adding/removing any keys while iterating through it.
I had a similar issue with wanting to change the dictionary's structure (remove/add) dicts within other dicts.
For my situation I created a deepcopy of the dict. With a deepcopy of my dict, I was able to iterate through and remove keys as needed.Deepcopy - PythonDoc
A deep copy constructs a new compound object and then, recursively, inserts copies into it of the objects found in the original.
Hope this helps!
You might want to copy your keys in a list and iterate over your dict using the latter, eg:
def expand(obj):
keys = obj.keys()
for k in keys:
expandField(obj, k, v)
I let you analyse if the resulting behavior suits your expected results.
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.