Last active
November 21, 2019 04:26
-
-
Save y56/210f0ae5915f496b1ec07b0b80863d58 to your computer and use it in GitHub Desktop.
Return None if Dictionary key is not available
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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