When deleting a key from a dictionary, I use:
if \'key\' in my_dict: del my_dict[\'key\']
Is there a one line way of doing this?
You can use a dictionary comprehension to create a new dictionary with that key removed:
>>> my_dict = {k: v for k, v in my_dict.items() if k != 'key'}
You can delete by conditions. No error if key doesn't exist.
key