In Python, you can remove an item (key-value pair) from a dictionary (dict
) using the clear()
, pop()
, popitem()
methods, or the del
statement. You can also remove items that satisfy the conditions using dictionary comprehensions.
Contents
- Remove all items from a dictionary: clear()
- Remove an item by key and return its value: pop()
- Remove an item and return its key and value: popitem()
- Remove an item by key: del
- Remove items that satisfy a condition: Dictionary comprehensions
See the following article for how to add items to a dictionary.
- Merge multiple dictionaries and add items to a dictionary in Python
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)# {}
source: dict_clear_pop_popitem_del.py
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.
Built-in Types - dict.pop() — Python 3.11.3 documentation
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'
source: dict_clear_pop_popitem_del.py
You can pass a second argument as a default value to pop()
. If the specified key doesn't exist, this 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
source: dict_clear_pop_popitem_del.py
Remove an item and return its key and value: popitem()
The popitem()
method removes an item from a dictionary and returns its key and value as a tuple, (key, value)
.
Since Python 3.7, the order of elements in the dictionary is preserved. As a result, popitem()
removes elements in LIFO (last in, first out) order.
A KeyError
is raised for an empty dictionary.
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 an item by key: del
You can also use the del
statement to delete an item from a dictionary.
d = {'k1': 1, 'k2': 2, 'k3': 3}del d['k2']print(d)# {'k1': 1, 'k3': 3}
source: dict_clear_pop_popitem_del.py
You can remove multiple items at once.
d = {'k1': 1, 'k2': 2, 'k3': 3}del d['k1'], d['k3']print(d)# {'k2': 2}
source: dict_clear_pop_popitem_del.py
If a non-existent key is specified, a KeyError
is raised.
d = {'k1': 1, 'k2': 2, 'k3': 3}# del d['k4']# print(d)# KeyError: 'k4'
source: dict_clear_pop_popitem_del.py
Remove items that satisfy a condition: Dictionary comprehensions
You can remove items that meet certain conditions from a dictionary using dictionary comprehensions, which are the dictionary equivalent of list comprehensions.
- List comprehensions in Python
Removing items that satisfy a condition is equivalent to extracting items that do not satisfy the condition.
For example, to remove items with odd values, you can extract items with even values. The same applies to the opposite case.
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}
source: dict_comprehension.py
The items()
method of dict
is used to extract keys and values.
- Iterate through dictionary keys and values in Python
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}
source: dict_comprehension.py
You can use and
and or
to specify multiple conditions.
- Boolean operators in Python (and, or, not)
dc = {k: v for k, v in d.items() if v % 2 == 0 and k.endswith('e')}print(dc)# {'orange': 100}
source: dict_comprehension.py