Skip to content

Instantly share code, notes, and snippets.

@matthewpoer
Last active June 17, 2021 20:30
Show Gist options
  • Save matthewpoer/998b2d54e96ef04b7d6804204d1ada41 to your computer and use it in GitHub Desktop.
Save matthewpoer/998b2d54e96ef04b7d6804204d1ada41 to your computer and use it in GitHub Desktop.
>>> dictionary = {"one":"one value", "two":"two value"}
>>> new_dictionary = {"foo":"foo value", "bar":"bar value"}
>>> dictionary.update(new_dictionary)
>>> print(dictionary)
{'one': 'one value', 'two': 'two value', 'foo': 'foo value', 'bar': 'bar value'}
>>> dictionary = {"one":"one value", "two":"two value"}
>>> new_dictionary = {"foo":"foo value", "bar":"bar value"}
>>> dictionary.update({**new_dictionary})
>>> print(dictionary)
{'one': 'one value', 'two': 'two value', 'foo': 'foo value', 'bar': 'bar value'}
>>> dictionary = {"one":"one value", "two":"two value"}
>>> new_dictionary = {"foo":"foo value", "bar":"bar value", "two":"updated two value"}
>>> dictionary.update(new_dictionary)
>>> print(dictionary)
{'one': 'one value', 'two': 'updated two value', 'foo': 'foo value', 'bar': 'bar value'}
@matthewpoer
Copy link
Author

A double asterisk ** denotes dictionary unpacking. Its operand must be a mapping. Each mapping item is added to the new dictionary. Later values replace values already set by earlier key/datum pairs and earlier dictionary unpackings.
https://docs.python.org/3/reference/expressions.html#dictionary-displays

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment