Here is an example to illustrate the problem...
a = {
"foo" : 2,
"bar" : 3,
}
b = {
"bar" : 4,
"zzz" : 5,
}
print(json.dumps(dict(a, **b), indent=4))
This gives you the following result...
{
"foo": 2,
"bar": 4,
"zzz": 5
}
Notice how the "foo"
key from a
was added to the result?
Now look at this example...
a = {
"foo" : {
"1" : {
"foo" : True,
},
"2" : {
"foo" : True,
},
"3" : {
"foo" : True,
},
"4" : {
"foo" : True
}
}
}
b = {
"foo" : {
"1" : {
"foo" : True,
},
"2" : {
"foo" : True,
},
"3" : {
"foo" : False,
}
}
}
print(json.dumps(dict(a, **b), indent=4))
This gives you the following result...
{
"foo": {
"1": {
"foo": true
},
"3": {
"foo": false
},
"2": {
"foo": true
}
}
}
"3"
was updated just like how "bar"
was updated in the previous example but notice how the "4"
key from a
was not added to the result like how "foo"
was in the previous example?
So my expected output would be:
{
"foo": {
"1": {
"foo": true
},
"3": {
"foo": false
},
"2": {
"foo": true
},
"4": {
"foo": true
}
}
}
How can I modify this dictionary union process so that keys in a
that are not in b
are retained?
My overall goal is to have my dictionary a
but with any values that are in b
overridden.