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


Categories

...