Join our newsletter for the latest updates.

Python Program to Delete an Element From a Dictionary

In this example, you will learn to delete an element from a dictionary.

To understand this example, you should have the knowledge of the following Python programming topics:


Example 1: Using del keyword

my_dict = {31: 'a', 21: 'b', 14: 'c'}

del my_dict[31]

print(my_dict)

Output

{21: 'b', 14: 'c'}

The above code removes the key value pair 31:'a' from the dictionary using the key.


Example 2: Using pop()

my_dict = {31: 'a', 21: 'b', 14: 'c'}

print(my_dict.pop(31))

print(my_dict)

Output

a
{21: 'b', 14: 'c'}

pop() also returns the value of the key passed.

Did you find this article helpful?