How to delete a key from a python dictionary

Published by admin on

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 details in this tutorial. We will learn how to delete a key-value pair in a dictionary.

 

The algorithm to use:

1. 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.

2. Print the dictionary to the user.

3. Ask the user to enter the key.

4. Read the key and store it in a variable.

5. 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.

6. 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) :

You can download the source code from here

 

python delete key from dictionary

 

Explanation :
The commented numbers in the above program denote the step number below :

1. dict is the given dictionary with different key-value pairs.

2. Print the dictionary to the user.

3. Ask the user to enter a key value that needs to be deleted. Read and store that value in the variable key_to_delete. We are using the input() method to read the user input data.

4. 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.

5. Finally, print out the dictionary again to the user.

 

Sample Examples :

python delete key from dictionary

Categories: python

0 Comments

Leave a Reply

Your email address will not be published. Required fields are marked *