Remove an item from a dictionary in Python (clear, pop, popitem, del)

Modified: | Tags: Python, Dictionary

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.

See the following article for how to add items to a dictionary.

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 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'

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

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}

You can remove multiple items at once.

d = {'k1': 1, 'k2': 2, 'k3': 3}

del d['k1'], d['k3']
print(d)
# {'k2': 2}

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'

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.

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}

The items() method of dict is used to extract 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}

Related Categories

Related Articles