Remove an Item from a Dictionary in Python: pop, popitem, clear, del
In Python, you can remove an item (key-value pair) from a dictionary (dict
) using the pop()
, popitem()
, clear()
methods, or the del
statement. You can also remove items based on specific conditions using dictionary comprehensions.
For instructions on how to add items to a dictionary, see the following article:
Remove an item by key and return its value: pop()
The pop()
method removes the item associated with the specified key from the dictionary and returns its value.
d = {'k1': 1, 'k2': 2, 'k3': 3}
removed_value = d.pop('k1')
print(d)
# {'k2': 2, 'k3': 3}
print(removed_value)
# 1
By default, specifying a non-existent key raises a KeyError
.
d = {'k1': 1, 'k2': 2, 'k3': 3}
# removed_value = d.pop('k4')
# print(d)
# KeyError: 'k4'
You can pass a second argument to pop()
as a default value. If the specified key doesn't exist, this default value is returned, and the dictionary remains unchanged.
d = {'k1': 1, 'k2': 2, 'k3': 3}
removed_value = d.pop('k4', None)
print(d)
# {'k1': 1, 'k2': 2, 'k3': 3}
print(removed_value)
# None
Remove an item and return its key and value: popitem()
The popitem()
method removes an item from the dictionary and returns its key and value as a tuple, (key, value)
.
Since Python 3.7, the order of items in the dictionary is preserved. As a result, popitem()
removes items in LIFO (last in, first out) order.
A KeyError
is raised if the dictionary is empty.
d = {'k1': 1, 'k2': 2}
k, v = d.popitem()
print(k)
print(v)
print(d)
# k2
# 2
# {'k1': 1}
k, v = d.popitem()
print(k)
print(v)
print(d)
# k1
# 1
# {}
# k, v = d.popitem()
# KeyError: 'popitem(): dictionary is empty'
Remove all items from a dictionary: clear()
The clear()
method removes all items from a dictionary, making it empty.
d = {'k1': 1, 'k2': 2, 'k3': 3}
d.clear()
print(d)
# {}
Remove an item by key: del
The del
statement can also be used to delete an item from a dictionary.
d = {'k1': 1, 'k2': 2, 'k3': 3}
del d['k2']
print(d)
# {'k1': 1, 'k3': 3}
You can remove multiple items at once using del
.
d = {'k1': 1, 'k2': 2, 'k3': 3}
del d['k1'], d['k3']
print(d)
# {'k2': 2}
A KeyError
is raised if a non-existent key is specified.
d = {'k1': 1, 'k2': 2, 'k3': 3}
# del d['k4']
# print(d)
# KeyError: 'k4'
Remove items based on a condition: Dictionary comprehensions
You can remove items that meet certain conditions from a dictionary using dictionary comprehensions, which are similar to list comprehensions but for dictionaries.
Removing items based on a condition is effectively the same as extracting items that do not meet the condition.
For example, to remove items with odd values, you can extract items with even values. The same logic applies if you want to remove items with even values and keep the odd ones.
d = {'apple': 1, 'banana': 10, 'orange': 100}
dc = {k: v for k, v in d.items() if v % 2 == 0}
print(dc)
# {'banana': 10, 'orange': 100}
dc = {k: v for k, v in d.items() if v % 2 == 1}
print(dc)
# {'apple': 1}
The items()
method of dict
is used to access both keys and values.
You can also specify conditions based on the keys.
dc = {k: v for k, v in d.items() if k.endswith('e')}
print(dc)
# {'apple': 1, 'orange': 100}
dc = {k: v for k, v in d.items() if not k.endswith('e')}
print(dc)
# {'banana': 10}
You can use and
and or
to specify multiple conditions.
dc = {k: v for k, v in d.items() if v % 2 == 0 and k.endswith('e')}
print(dc)
# {'orange': 100}