Created
August 2, 2023 15:19
-
-
Save e-dreyer/eb421026e66ed4ad92140803df95dbfe to your computer and use it in GitHub Desktop.
Zip copy missing keys
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 copy_missing_keys(source_dict, destination_dict): | |
for key, value in source_dict.items(): | |
if key not in destination_dict: | |
destination_dict[key] = value | |
# Sample dictionaries | |
dict1 = {'key1': 42, 'key2': 10, 'key3': 5} | |
dict2 = {'key2': 15, 'key4': 23} | |
# Copy missing keys and values from dict1 to dict2 | |
copy_missing_keys(dict1, dict2) | |
print(dict2) | |
# Output | |
{'key2': 15, 'key4': 23, 'key1': 42, 'key3': 5} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment