Guest User

Untitled

a guest
Jul 18th, 2018
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.60 KB | None | 0 0
  1. def merge_dicts(d1, d2):
  2. """Safely merges the contents of the dictionary d2 into dictionary d1.
  3.  
  4. Unlike dict.update(), this is a non-destructive operation: a KeyError is
  5. raised if one of d2's keys already exists in d1.
  6. """
  7. # Based on timing tests, it's faster to scan for conflicts upfront and
  8. # then perform a bulk update. This presumes that, generally speaking,
  9. # the destination dictionary d1 will be larger (in item count) than d2.
  10. for key in d2.iterkeys():
  11. if key in d1:
  12. raise KeyError("destination dict already contains key '%s'" % key)
  13. d1.update(d2)
Add Comment
Please, Sign In to add comment