Python beginner here. I have a dictionary with a key and its value is an object (dict) who also has a key value pair. I want to add a key value pair to the 'child' object.
given:
{"foo" :
{"bar" : "bars value"}
}
I want to add:
{"foo" :
{"bar" : "bar value",
"baz" : "baz value"
}
}
This seems incredibly common but I can't seem to find a good way to do it.
somedict = {"foo" :
{"bar" : "bars value"}
}
somedict['foo']['baz'] = 'baz value'
When Python encounters somedict['foo']['baz']
it first looks-up the value of the bare name somedict
. It finds it is a dict
. Then it evaluates somedict['foo']
and finds it is another dict. Then it assigns to this dict a new key 'baz'
with value `baz value'
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With