Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
4 views
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)

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...