Introduction
In this post, I am going to write a python program to delete an element from a dictionary. The main objective of this program is to delete an element from a dictionary. But, two extra programs will be there. First is to clear whole dictionary and other is to delete whole dictionary object.
Also Read: Python Program to Check Armstrong Number
Let us see one by one.
Python Program to Delete an Element From a Dictionary
Here, we will ask user to enter a key. After that, we will delete the value which is associated with this key.
Now, see the following program.
# Python Program to Delete an Element from a Dictionary input_dict = {1 : 'Virat Kohali', 2 : 'Rohit Sharma', 3 : 'Suryakumar Yadav', 4 : 'Shardul Thakur'} print("Given Dictionary : ", input_dict) key = int(input("Enter the key to delete: ")) if key in input_dict.keys(): del input_dict[key] print("Dictionary After Deletion: ", input_dict) else: print("Sorry, You have entered wrong key....")
The above python program is deleting a particular element from the dictionary. See the following steps:
- Create or declare a dictionary.
- Ask the user to enter the key which we want to delete.
- Now, check if that key is availble in the given dictionary. If yes, then go to the step 4, otherwise, display invalid key message.
- Delete the respective value for the given key.
As you can see, these are so simple steps and program is also very easy. See the following output.
Output

I hope you have understood what we have done here.
Here, we are deleting an element from a dictionary.
Also Read: Python Program to Check If Two Strings are Anagram
Clear the Dictionary
Now, we will clear the dictionary. We are not going to delete the dictionary. After clearing, only empty dictionary will be left.
For this, we will clear function.
# Python Program to Clear the Dictionary input_dict = {1 : 'Virat Kohali', 2 : 'Rohit Sharma', 3 : 'Suryakumar Yadav', 4 : 'Shardul Thakur'} print("Given Dictionary : ", input_dict) input_dict.clear() print("Given Dictionary After Clearing: ", input_dict)
Output

Delete the Whole Dictionary
First, we deleted the particular element from the dictionary. Next, we cleared the whole dictionary. After clearing, only empty dictionary will be there.
Now, we are going to delete the complete dictionary. There will be no reference in the memory.
# Python Program to Delete the Dictionary input_dict = {1 : 'Virat Kohali', 2 : 'Rohit Sharma', 3 : 'Suryakumar Yadav', 4 : 'Shardul Thakur'} print("Given Dictionary : ", input_dict) del input_dict print("Given Dictionary After Clearing: ", input_dict)
Output

See the above ouput. There is an error that input_dict is not defined. But if you see in the program, input_dict is already there. But we deleted that dictionary using del keyword.
After this we are trying to display the dictionary. But there is no reference in the memory.
I hope, you have understood all these programs. These are very easy programs. But still if you have difficulties regarding these programs, please let me know.