Explore Courses Blog Tutorials Interview Questions
0 votes
in Python by (47.6k 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 (106k points)

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

my_dict.pop('key', None)

Browse Categories