How can I remove a key from a Python dictionary?

Is there a one-line way of deleting a key from a dictionary without raising a KeyError?

if 'key' in my_dict:
    del my_dict['key']

1
15

Leave a Comment