Back

Explore Courses Blog Tutorials Interview Questions
0 votes
1 view
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)
0 votes
1 answer
+1 vote
1 answer
asked Jul 31, 2019 in Python by Eresh Kumar (45.3k points)
0 votes
1 answer
Welcome to Intellipaat Community. Get your technical queries answered by top developers!

28.4k questions

29.7k answers

500 comments

94.1k users

Browse Categories

...