10 minutes read

Python program to remove the key from a dictionary


Problem Statement: Write a Python Program to remove the key from a dictionary.

Description: Dictionary is key-value pair data structures in Python. We can get the value of the dictionary through its key.

Here is the syntax of the Python dictionary.

some_dict = {'a': 1, 'b': 2}

Here a and b are keys with values 1 and 2 respectively.

This is how a dictionary in Python looks. The value of a dictionary can be extracted using a key like some_dict['a'] will print 1.

Python Program: Remove the key from a dictionary


my_dict = {'name': 'Digvijay', 'age': 24, 'languages':['Python', 'Java', 'Javascript']}
print("Original Dictionary ",my_dict)
key_to_del = 'languages'
if(key_to_del in my_dict):

print("After deletion Dictionary ",my_dict)


Original Dictionary {'name': 'Digvijay', 'age': 24, 'languages': ['Python', 'Java', 'Javascript']} After deletion Dictionary {'name': 'Digvijay', 'age': 24}

The logic of the program is simple. We first declare a variable with a dictionary.

We print the original dictionary (before deletion). The next step is to check if the key exists in the dictionary, it is important to avoid error keyError.

In the next step, we delete the key-value pair.

Finally, we print the dictionary and compare the results.

Here are some related Python Examples:

Join Our Youtube Channel