0 votes
1 view
in Python by (47.8k points)

When deleting a key from a dictionary, I use:

if 'key' in myDict: 

del myDict['key']

Is there a one-line way of doing this?

1 Answer

0 votes
by (107k points)

To remove a key from a dictionary, you can use the two-argument form of dict.pop():

my_dict.pop('key', None)

Welcome to Intellipaat Community. Get your technical queries answered by top developers !