0

I am implementing script to generate Json config from nested dictionary. So I should get a value from some file, change nested dictionary values and then convert it to the Json. And here is the problem. I know how to extract a value (thanks to stackoverflow) from a nested dictionary. This is the way:

input2 = {'key1': {'key11': {'key111': {'key1111': 5}}}, 'key2': 'value2', 'key3': 'value3'}
print(reduce(getitem, ['key1', 'key11'], input2))

Now I would like to replace for example 'key111' with something else, I can do it this way: input2['key1']['key11']['key111'] = 1, however how can I do that with a variable ?

Something like

xxx = ['key1']['key11']['key111']
input2[xxx] = 1

I haven't found the answer from the search, sorry.

0 Answers0