The most efficient way to remove a key from a dictionary in Python is to use the del keyword. The del keyword removes the given key from the dictionary and can also delete the entire dictionary.
new_dict = {'Ronaldo': 7, 'Messi': 10, 'Neymar': 11, 'Lloris': 1}
print("Before removing a key:", new_dict)
# Output: Before removing a key: {'Ronaldo': 7, 'Messi': 10, 'Neymar': 11, 'Lloris': 1}
del new_dict['Neymar']
print("After removing a key:", new_dict)
# Output: After removing a key: {'Ronaldo': 7, 'Messi': 10, 'Lloris': 1}
In this code, we want to remove a key-value pair whose key is “Neymar”. We selected a specific key using indexing and used the del keyword to remove it from a dictionary.
Key doesn’t exist
If the given key does not exist in the dictionary, it will throw a KeyError.
main_dict = {'a': 11, 'b': 22}
del main_dict['c']
# Output: KeyError: 'c'
Prevent the KeyError by checking if the “c” key exists using the “in” operator before attempting to delete it.
main_dict = {'a': 11, 'b': 22}
if 'c' in main_dict:
del main_dict['c']
else:
print("Key 'c' not found")
# Output: Key 'c' not found
Empty dictionary
If the input dictionary is empty and you attempt to check for a specific key, it results in a KeyError as well because the key does not exist in the dictionary.
main_dict = {}
del main_dict['a']
# Output: KeyError: 'a'
Removing Keys from a nested dictionary
When it comes to a nested dictionary, you can either remove the specific key of the nested dictionary or remove the whole nested dictionary.
Let’s remove the “inner” key:
main_dict = {'outer': {'inner': 1}}
# Remove the "inner" key from the nested dictionary
del main_dict['outer']['inner']
print(main_dict)
# Output: {'outer': {}}
Since it removed the inner dictionary, the outer dictionary is unaffected.
However, we can empty the whole dictionary if we remove the “outer” key like this:
main_dict = {'outer': {'inner': 1}}
# Removing the "outer" key
del main_dict['outer']
print(main_dict)
# Output: {}
If you remove the “outer” key, the nested key “inner” is also deleted, which is something you need to consider when you are dealing with a nested dict.
Alternate approaches
Using pop()
The pop() method removes the specified key from the dictionary and returns its value.
If the given key doesn’t exist in the dictionary, it returns the specified default value.
If the key is not in the dictionary and no default value is provided, it will throw a KeyError.
new_dict = {'Ronaldo': 7, 'Messi': 10, 'Neymar': 11, 'Lloris': 1}
print("Before removing key:", new_dict)
# Output: Before removing key: {'Ronaldo': 7, 'Messi': 10, 'Neymar': 11, 'Lloris': 1}
value = new_dict.pop('Lloris')
print("Value of removed key:", value)
# Output: Value of removed key: 1
print("After removing key:", new_dict)
# Output: After removing key: {'Ronaldo': 7, 'Messi': 10, 'Neymar': 11}
With Default Value
If the key does not exist but you provide a default value, it will return the default value without generating any error.
main_dict = {'a': 1}
value = main_dict.pop('b', 'Not Found') # Returns 'Not Found' (no KeyError)
print(value)
# Output: 'Not Found'
print(main_dict)
# Output: {'a': 1}
Remove multiple keys from the dictionary
To remove multiple keys from an input dictionary, you need to create a list of dictionaries, loop over a list of keys, and use the pop() method to remove them one by one.
main_dict = {"Name": "Harvey", "City": "New York",
"Age": 36, "Profession": "Lawyer"}
# List of keys to remove
keys = ['Age', 'City', 'Profession']
for key in keys:
main_dict.pop(key, None)
print(main_dict)
# Output: {'Name': 'Harvey'}
In this code, we removed three keys using a for loop and the pop() method: ‘City’, ‘Age’, and ‘Profession’.
The output dictionary contains only a single property, ‘Name’.
Using popitem()
The popitem() method removes and returns the last key-value pair from the dictionary.
new_dict = {'Ronaldo': 7, 'Messi': 10, 'Neymar': 11, 'Lloris': 1}
print("Before removing the last key:", new_dict)
# Output: Before removing the last key: {'Ronaldo': 7, 'Messi': 10, 'Neymar': 11, 'Lloris': 1}
removed_item = new_dict.popitem()
print("Removed element:", removed_item)
# Output: Removed element: ('Lloris', 1)
print("After removing the last key:", new_dict)
# Output: After removing the last key: {'Ronaldo': 7, 'Messi': 10, 'Neymar': 11}
Keys in the dictionary must be hashable; attempting to remove non-hashable (e.g., list) raises TypeError on access.
main_dict = {'a': 1}
value = main_dict.pop([1, 2], 'Not Found') # Returns TypeError
print(value)
# Output: TypeError: unhashable type: 'list'
Since the key we are trying to delete is a list [1, 2], which is an unhashable element, we got the TypeError.





