Home
Head's Up: I'm in the middle of upgrading my site. Most things are in place, but there are something missing and/or broken including image alt text. Please bear with me while I'm getting things fixed.

Delete A Key From A Dictionary In Python

Top recommened way to delete a key in a python dictionary is :

(via : https : //stackoverflow.com/a/11277439/102401)

To delete a key regardless of whether it is in the dictionary, use the two - argument form of dict.pop() :

my _ dict.pop('key', None)

This will return my _ dict[key] if key exists in the dictionary, and None otherwise. If the second parameter is not specified (ie. my _ dict.pop('key')) and key does not exist, a KeyError is raised.

To delete a key that is guaranteed to exist, you can also use

del my _ dict['key'] This will raise a KeyError if the key is not in the dictionary.

Specifically to answer "is there a one line way of doing this?"

if 'key' in my _ dict : del my _ dict['key'] ...well, you asked ; - )

You should consider, though, that this way of deleting an object from a dict is not atomic—it is possible that 'key' may be in my _ dict during the if statement, but may be deleted before del is executed, in which case del will fail with a KeyError. Given this, it would be safest to either use dict.pop or something along the lines of

try : del my _ dict['key'] except KeyError : pass which, of course, is definitely not a one - liner.