How to Remove Key from Python Dictionary?

How to Remove Key from Python Dictionary?

Blog | Board Infinity
Blog | Board Infinity

Introduction

Its numerous practical uses in everyday software programming make the dictionary a handy container in general. Therefore, having shortcuts to do various activities linked to dictionary usage is always advantageous. In this article, we'll cover various approaches to dealing with the given task of deleting or removing a dictionary key-value combination from a dictionary. Finally, we'll look at how to delete all the keys from the dictionary.

Methods to Delete Keys from a Dictionary

Python uses a collection called a dictionary to store data as Key: Value pairs.

Example:

dict = { "python" : 1, "C++" : 3, "javaScript" : 2 }

The keys to be deleted from our dictionary data structure will be determined by the user's input. To remove this key from the vocabulary and print the revised dictionary, a script must be written. The key must be located and taken out. Python has several ways on collections that may be used to do this. Let's watch them in action.

Input:
dict = { "python" : 1, "C++" : 3, "javaScript" : 2 }
python

Output:
After removal : { "C++" : 3, "javaScript" : 2 }

Method 1: Using pop() method on dictionary

The pop() function may be used to delete any key from Python. A dictionary's pop() function is used to remove a key: value pair. The method outputs the string that we can supply as an additional parameter and returns the removed pair if it doesn't exist (an exception is thrown otherwise).

Syntax:

dictionary_name.pop("key")

Code Example

langDict = {"python" : 24, "c++" : 22, "javaScript" : 25, "java" : 20, "r" : 21 }
print("The dictionary is :", langDict)
erase = input("Enter the key to be deleted ")
delVal = langDict.pop(erase, "Wrong key entered")
print("Deleting Key ", langDict)
print("The deleted value is :", delVal)

Output:

The dictionary is : {'java': 20, 'r': 21, 'c++': 22, 'python': 24, 'javaScript': 25}
Enter the key to be deleted java
Deleting Key  {'r': 21, 'c++': 22, 'python': 24, 'javaScript': 25}
The deleted value is : 20

Method 2: Using del

Using the del function is another way to remove a key from a dictionary. To remove a particular key from the dictionary, use the del keyword and the dict[key] notation. In the event of a false value, del does not allow any further value to be returned. Instead, it will produce an error.

Code Example

langDict = {"python" : 24, "c++" : 22, "javaScript" : 25, "java" : 20, "r" : 21 }
print("The dictionary is :", langDict)
erase = input("Enter the key to be deleted ")
del langDict[erase]
print("Deleting Key ", langDict)

Output:

The dictionary is : {'python': 24, 'java': 20, 'javaScript': 25, 'c++': 22, 'r': 21}
Enter the key to be deleted r
Deleting Key  {'python': 24, 'java': 20, 'javaScript': 25, 'c++': 22}

Method 3: Using iterator/comprehension

While building a new dictionary in Python using comprehension, we'll filter out the key:val.While building a new dictionary in Python using comprehension, we'll filter out the key:val.

Code Example

langDict = {"python" : 24, "c++" : 22, "javaScript" : 25, "java" : 20, "r" : 21 }
print("The dictionary is :", langDict)
erase = input("Enter the key to be deleted ")
newDict = { key:val for key, val in langDict.items() if key != erase}
print("Deleting Key ", newDict)

Output:

The dictionary is : {'c++': 22, 'javaScript': 25, 'python': 24, 'r': 21, 'java': 20}
Enter the key to be deleted r
Deleting Key  {'c++': 22, 'javaScript': 25, 'python': 24, 'java': 20}

Explanation

Using comprehension, we removed a key from the dictionary in the code above. A built-in feature called comprehension enables the programmer to complete the task with fewer lines of code. With understanding, we have eliminated the key from the vocabulary that we wish to remove. For this, we loop through the dictionary's simple entries, adding them to a new dictionary if the key does not match the key that will be erased.

Python

Blog | Board Infinity

At Board Infinity we have authors leading in their profession sharing their insights, ideas and inspiration. Here influential thinkers, creators, makers and doers are found in one place.