When trying to delete a key from a dictionary, I write:
if 'key' in myDict:
del myDict['key']
Is there a one line way of doing this?
When trying to delete a key from a dictionary, I write:
if 'key' in myDict:
del myDict['key']
Is there a one line way of doing this?
It took me some time to figure out what exactly
my_dict.pop("key", None)
is doing. So I'll add this as an answer to save others googling time:Documentation
Using the "del" keyword: