Skip to content

Instantly share code, notes, and snippets.

@y56
Last active November 21, 2019 04:26
Show Gist options
  • Save y56/210f0ae5915f496b1ec07b0b80863d58 to your computer and use it in GitHub Desktop.
Save y56/210f0ae5915f496b1ec07b0b80863d58 to your computer and use it in GitHub Desktop.
Return None if Dictionary key is not available
https://stackoverflow.com/questions/6130768/return-none-if-dictionary-key-is-not-available
You can use dict.get()
value = d.get(key)
which will return None if key is not in d. You can also provide a different default value that will be returned instead of None:
value = d.get(key, "empty")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment