Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
4 views
in Python by (4k points)
edited by

How can I delete an element from a dictionary?

In order to return a copy, how can I delete an element/item from a dictionary (without modifying the original)?

1 Answer

0 votes
by (119k points)

You can use the following syntax to delete the element from Dictionary:

del d[key]

The above code mutates the existing dictionary but if you want to return a new dictionary, make a copy of the dictionary using the following code:

def removekey(d, key):

r = dict(d)

del r[key] 

           return r

If you want to learn handling dictionaries then check out this Python Tutorial by Intellipaat. 

Related questions

0 votes
1 answer
asked Jul 16, 2019 in Python by leealex956 (7.3k points)
+1 vote
1 answer
asked Jul 31, 2019 in Python by Eresh Kumar (45.3k points)
0 votes
1 answer
0 votes
1 answer

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...