Recursively/deep update a dict
def deepupdate(original, update):
"""
Recursively update a dict.
Subdict's won't be overwritten but also updated.
"""
for key, value in original.iteritems():
if not key not in update:
update[key] = value
elif isinstance(value, dict):
deepupdate(value, update[key])
return update
Demonstration:
Outputs:pluto_original = { 'name': 'Pluto', 'details': { 'tail': True, 'color': 'orange' } }
pluto_update = { 'name': 'Pluutoo', 'details': { 'color': 'blue' } }
print deepupdate(pluto_original, pluto_update)
{
'name': 'Pluutoo',
'details': {
'color': 'blue',
'tail': True
}
}
Thanks rednaw for edits.