This is a follow up to following question on SO:
Access nested dictionary items via a list of keys?
All the solutions given in the above link allows to replace/create a value of the nested dict via a list of keys. But my requirement is to append (update) another dict to value.
If my dict is as follows:
dataDict = {
"a":{
"r": 1,
"s": 2,
"t": 3
},
"b":{
"u": 1,
"v": {
"x": 1,
"y": 2,
"z": 3
},
"w": 3
}
}
And my list of keys is as follows:
maplist = ["b", "v"]
How can I append another dict, say "somevalue": {}
to the dataDict['b']['v']
using my maplist?
So, after appending new dataDict would look as follows:
dataDict = {
"a":{
"r": 1,
"s": 2,
"t": 3
},
"b":{
"u": 1,
"v": {
"x": 1,
"y": 2,
"z": 3,
"somevalue": {}
},
"w": 3
}
}
The solutions on the aforementioned SO link changes/creates the value like this:
dataDict[mapList[-1]] = value
But we can't call the dict's update function like dataDict[mapList[-1]].update()
.
Checked all of SO and Google, but no luck. Could someone please provide some help with this. Thanks!