Back

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

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

28.4k questions

29.7k answers

500 comments

94k users

Browse Categories

...