Skip to content

Instantly share code, notes, and snippets.

@pavelanni
Created March 12, 2021 15:54
Show Gist options
  • Save pavelanni/800a1de2d7c85881b751868208f1d5e3 to your computer and use it in GitHub Desktop.
Save pavelanni/800a1de2d7c85881b751868208f1d5e3 to your computer and use it in GitHub Desktop.
Python: Add items to a dict from a second dict without changing the existing ones
d1 = {'a': 1, 'b': 2, 'c': 3}
d2 = {'d': 4, 'e': 5, 'a': 6}
d1.update({k:v for (k,v) in d2.items() if k not in d1})
d1
# Out: {'a': 1, 'b': 2, 'c': 3, 'd': 4, 'e': 5}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment