How to delete a key from a python dictionary

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:

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

#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)

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 keytodelete. 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 __ View on Github

Similar tutorials :

Where is the color and why codevscolor ?

Long story short, I love paintings and I paint on weekends. We(me and my wife) have one Youtube channel. Below is a video that I did recently. If you love this please do subscribe to support us 😊❤️❤️