Python - Remove Item from Dictionary
Last Updated :
27 Jan, 2025
There are situations where we might want to remove a specific key-value pair from a dictionary. For example, consider the dictionary d = {'x': 10, 'y': 20, 'z': 30}. If we need to remove the key 'y', there are multiple ways to achieve this. Let's discuss several methods to remove an item from a dictionary.
Using pop()
pop() method is the most direct and efficient way to remove an item from a dictionary when the key is known. It also allows retrieving the removed value.
Python
d = {'x': 10, 'y': 20, 'z': 30}
# Remove item using pop()
removed_value = d.pop('y')
print(d)
print(removed_value)
Output{'x': 10, 'z': 30}
20
Explanation:
- The pop() method removes the specified key from the dictionary.
- It returns the value associated with the removed key.
- If the key does not exist, it raises a KeyError unless a default value is provided as the second argument (e.g., d.pop('key', None)).
Let's explore some more ways and see how we can remove item from dictionary.
Using del()
del() statement is another efficient way to remove a key-value pair when the key is known. Unlike pop(), it does not return the removed value.
Python
d = {'x': 10, 'y': 20, 'z': 30}
# Remove item using del
del d['y']
print(d)
Explanation:
- The del() statement deletes the specified key and its associated value from the dictionary.
- If the key does not exist, it raises a KeyError.
Using Dictionary Comprehension
We can create a new dictionary excluding the key-value pair to be removed. This method is useful when we need to avoid modifying the original dictionary.
Python
d = {'x': 10, 'y': 20, 'z': 30}
# Remove item using dictionary comprehension
d = {k: v for k, v in d.items() if k != 'y'}
print(d)
Explanation:
- The dictionary comprehension iterates over the key-value pairs in the original dictionary.
- It filters out the key-value pair where the key matches 'y'.
- A new dictionary is created without modifying the original one.
Using popitem()
popitem() method removes the last key-value pair from the dictionary. It is useful for scenarios where the order of removal does not matter (in Python 3.7+ where dictionaries preserve insertion order).
Python
d = {'x': 10, 'y': 20, 'z': 30}
# Remove the last item using popitem()
removed_item = d.popitem()
print(d)
Explanation:
- The popitem() method removes and returns the last inserted key-value pair as a tuple.
- If the dictionary is empty, it raises a KeyError.
Using clear()(To Remove All Items)
If the goal is to remove all key-value pairs from a dictionary, the clear() method is the simplest option.
Python
d = {'x': 10, 'y': 20, 'z': 30}
# Remove all items using clear()
d.clear()
print(d)
Explanation:
- The clear() method removes all items from the dictionary, leaving it empty.
- This is more efficient than iterating through the dictionary and removing keys individually.
Similar Reads
Python Remove Dictionary Item Sometimes, we may need to remove a specific item from a dictionary to update its structure. For example, consider the dictionary d = {'x': 100, 'y': 200, 'z': 300}. If we want to remove the item associated with the key 'y', several methods can help achieve this. Letâs explore these methods.Using pop
2 min read
Python Remove Item from Dictionary by Key Dictionaries in Python store data as key-value pairs. Often, we need to remove a specific key-value pair to modify or clean the dictionary. For instance, consider the dictionary d = {'a': 1, 'b': 2, 'c': 3}; we might want to remove the key 'b'. Let's explore different methods to achieve this.Using d
3 min read
Python Remove Item from Dictionary by Value We are given a dictionary and our task is to remove key-value pairs where the value matches a specified target. This can be done using various approaches, such as dictionary comprehension or iterating through the dictionary. For example: d = {"a": 10, "b": 20, "c": 10, "d": 30} and we have to remove
3 min read
Remove Kth Key from Dictionary - Python We are given a dictionary we need to remove Kth key from the dictionary. For example, we are given a dictionary d = {'key1': 'value1', 'key2': 'value2', 'key3': 'value3', 'key4': 'value4'} we need to remove the key2 so that the output should be {'key1': 'value1', 'key3': 'value3', 'key4': 'value4'}.
3 min read
Python - Remove Top level from Dictionary Sometimes, while working with Python Dictionaries, we can have nesting of dictionaries, with each key being single values dictionary. In this we need to remove the top level of dictionary. This can have application in data preprocessing. Lets discuss certain ways in which this task can be performed.
3 min read