Created
June 28, 2019 07:35
-
-
Save traderbagel/7c1d6fc5136306f9963d7875bf43bdce to your computer and use it in GitHub Desktop.
python delete dictionary key #python
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
# 1. get deleted value | |
my_dict.pop('key', None) | |
# 2. simple but not atomic | |
if 'key' in my_dict: del my_dict['key'] | |
# 3. atomic | |
try: | |
del my_dict['key'] | |
except KeyError: | |
pass |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment