Remove Key from Dictionary Python
We know that the elements in the dictionary are comprised of key-value pairs. With the help of these keys, the corresponding values can be easily fetched.
In this article, we will learn how we can remove the key from the dictionary in Python.
We will use the following methods to do the same-
- Using del()
- Using pop()
- Using items() and dictionary comprehension
- Using loop to remove multiple keys
So, let’s get started with the first one!
Using del()
The following program shows how we can remove a key from dictionary using del().
Output:
The values present in the dictionary are: {'English': 129901, 'Mathematics': 129902, 'Physics': 129903, 'Chemistry': 129904, 'Computer Science': 129905} The values present in the dictionary after removal are: {'English': 129901, 'Mathematics': 129902, 'Physics': 129903, 'Computer Science': 129905}
Explanation-
Let’s understand what we have done in the above program-
- We have initialized the key-value pairs of our dictionary in the first step.
- After this, we have displayed our dictionary.
- In the next step, we have deleted the key ‘Chemistry’ using del from the dictionary subj_code.
- On executing the program, the expected output is displayed that is, dictionary after the removal of key.
Now, in the second program, we will learn how pop() can be used.
Using pop()
Consider the program given below,
Output:
The values present in the dictionary are: {'English': 129901, 'Mathematics': 129902, 'Physics': 129903, 'Chemistry': 129904, 'Computer Science': 129905} The value of the removed key: 129902 The values present in the dictionary after removal are: {'English': 129901, 'Physics': 129903, 'Chemistry': 129904, 'Computer Science': 129905}
Explanation-
It’s time to have a look at the explanation of this program,
- We have initialized the key-value pairs of our dictionary in the first step.
- After this, we have displayed our dictionary.
- In the next step, we have used the pop() method and specified the key that we would like to remove.
- On executing the program, the expected output is displayed that is, dictionary after the removal of the key.
Now, let’s see how dictionary comprehension can help us with this,
Using items() and Dictionary Comprehension
The program given below demonstrates how it can be done,
Output:
The values present in the dictionary are: {'English': 129901, 'Mathematics': 129902, 'Physics': 129903, 'Chemistry': 129904, 'Computer Science': 129905} The values present in the dictionary after removal are: {'Mathematics': 129902, 'Physics': 129903, 'Chemistry': 129904, 'Computer Science': 129905}
Explanation-
Let’s check what happened here,
- We have initialized the key-value pairs of our dictionary in the first step.
- After this, we have displayed our dictionary.
- In the next step, we have used dictionary comprehension that takes all the key-value pairs from subj_code except the one that we would like to remove.
- On executing the program, the expected output is displayed that is, dictionary after the removal of the key.
Finally, in the last program, we will see how a loop can be used to remove multiple keys from a dictionary.
Using loop to Remove Multiple Keys
The following illustrates the same-
Output:
The dictionary after removal of keys is: {'Mathematics': 129902, 'Chemistry': 129904, 'Computer Science': 129905}
Explanation-
Let’s understand what we have done in the above program-
- We have initialized the key-value pairs of our dictionary in the first step.
- After this, we have displayed our dictionary.
- Now we have declared a dictionary that contains the keys we would like to remove and also the ones which don’t exist in our dictionary.
- In the next step, we have used for loop that removes the specified keys from the dictionary using pop(). (We have mentioned ‘None’ here so that the KeyError is not generated when the key is not found).
- On executing the program, the expected output is displayed that is, dictionary after the removal of multiple keys.
Conclusion
In this article, we came across another interesting task based on dictionaries and learned the different ways of removing keys from it in Python.