Skip to content

Instantly share code, notes, and snippets.

@anselmobd
Last active January 28, 2020 18:52
Show Gist options
  • Save anselmobd/5f86699b5b47aface5046c3502f354aa to your computer and use it in GitHub Desktop.
Save anselmobd/5f86699b5b47aface5046c3502f354aa to your computer and use it in GitHub Desktop.
Like dict.update, but takes care of going through several levels of dicts.
def update_dict(original, adding):
result = original.copy()
for key in adding.keys():
if adding[key] is None:
continue
if isinstance(adding[key], dict):
if key not in result or not isinstance(result[key], dict):
result[key] = adding[key].copy()
else:
result[key] = update_dict(result[key], adding[key])
else:
result[key] = adding[key]
return result
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment