Skip to content

Instantly share code, notes, and snippets.

@e-dreyer
Created August 2, 2023 15:19
Show Gist options
  • Save e-dreyer/eb421026e66ed4ad92140803df95dbfe to your computer and use it in GitHub Desktop.
Save e-dreyer/eb421026e66ed4ad92140803df95dbfe to your computer and use it in GitHub Desktop.
Zip copy missing keys
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