Open In App

Python - Remove Item from Dictionary

Last Updated : 27 Jan, 2025
Summarize
Comments
Improve
Suggest changes
Share
Like Article
Like
Report

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)

Output
{'x': 10, 'z': 30}

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)

Output
{'x': 10, 'z': 30}

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) 

Output
{'x': 10, 'y': 20}

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)

Output
{}

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.

Next Article

Similar Reads