Last active
January 28, 2020 18:52
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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