Python program to delete a key from a dictionary :
In this tutorial, we will learn how to delete a key _from a dictionary in python 3. The Python dictionary is a _mutable and unordered collection. Dictionaries are used to store key-value _pairs in Python. Curly braces _{} are used to define a dictionary in Python. Each key and its value are separated by a colon ( : ).
Keys are used for accessing an item in a dictionary. The key is used inside a square bracket _with the dictionary _name. For example, dict[“k”] will return us the value for key “k” _in the dictionary _“dict”. Python Dictionary also has one method get() to get the value for a key. This method takes the key as its argument and returns the value for that key.
Dictionary is a mutable collection. We can use the key to change the value of a specific item. For example, dic[“k”] = val _will assign the value of the key _“k” in the dictionary “dic” to “val”.
We can also delete an item in a dictionary using a key. ’del’ keyword is used for that. ’del dic[“k”]’ is used to delete the key-value pairs for the key ’k’ in the dictionary ’dic‘.
To determine if a key is present in a dictionary or not, ’in’ keyword is used. Using this keyword, we can verify if a key exists in a dictionary or not before trying to access it. Python dictionary provides a lot of other methods but we are not going to discuss it in detail in this tutorial. We will learn how to delete a key-value pair in a dictionary.
The algorithm to use:
- Create one dictionary variable with predefined_ key-value_ pairs. We are using one predefined dictionary in this example. But if you want, you can read the dictionary values from the user.
- Print the dictionary to the user.
- Ask the user to enter the key.
- Read the key and store it in a variable.
- Check if the key exists in the dictionary or not. If it does, delete the key-value pair for that key. Else, print one message to the user.
- Print the final modified dictionary to the user.
Let’s take a look at the program :
Python 3 program to delete a dictionary key-value pair (we are using the above algorithm in this program) :
#1
dict = {'one':1,'two':2,'three':3,'four':4,'five':5,'six':6,'seven':7,'eight':8,'nine':9,'ten':10}
#2
print("Current Dictionary : ",dict)
#3
key_to_delete = input("Enter the key to delete : ")
#4
if key_to_delete in dict:
del dict[key_to_delete]
else :
print("Please enter a valid key.")
#5
print("Final Dictionary ",dict)
Explanation :
The commented numbers in the above program denote the step number below :
- dict is the given dictionary with different key-value pairs.
- Print the dictionary to the user.
- Ask the user to enter a key value that needs to be deleted. Read and store that value in the variable keytodelete. We are using the input() method to read the user input data.
- Check if the key exists in the dictionary or not. If yes, delete the key-value pair using del dict[key] method. Else, ask the user to enter a valid key. As explained above, ’del’ is used to delete the key-value pairs in a dictionary.
- Finally, print out the dictionary again to the user.
Sample Examples :
Similar tutorials :
- Python program to find the sum of all values of a dictionary
- How to create a dictionary from two lists in python
- Python dictionary example to find keys with the same value
- Python print dictionary keys and values
- Python find the key of a dictionary with maximum value
- Sort a python dictionary by values