Skip to content

Add and Remove in Dictionary

In Python, we can add items in a dictionary using the following ways.

Add a new key-value pair using [] operator

Section titled “Add a new key-value pair using [] operator”

We can add a new key-value pair in a dictionary using the [] operator. If the key already exists in the dictionary, then the value of the key is updated. If the key does not exist in the dictionary, then a new key-value pair is added to the dictionary.

dict_add.py
data = {'a': 1, 'b': 2, 'c': 3, 'd': 4}
data['e'] = 5
print(data)

Output:

command
C:\Users\username>python dict_add.py
{'a': 1, 'b': 2, 'c': 3, 'd': 4, 'e': 5}

In this example, we declare a dictionary and assign it to the variable data. We then add a new key-value pair to the dictionary using the [] operator. The output shows that the new key-value pair is added to the dictionary.

Add a new key-value pair using update() method

Section titled “Add a new key-value pair using update() method”

We can add a new key-value pair in a dictionary using the update() method. If the key already exists in the dictionary, then the value of the key is updated. If the key does not exist in the dictionary, then a new key-value pair is added to the dictionary.

dict_update.py
data = {'a': 1, 'b': 2, 'c': 3, 'd': 4}
data.update({'e': 5})
print(data)

Output:

command
C:\Users\username>python dict_update.py
{'a': 1, 'b': 2, 'c': 3, 'd': 4, 'e': 5}

In this example, we declare a dictionary and assign it to the variable data. We then add a new key-value pair to the dictionary using the update() method. The output shows that the new key-value pair is added to the dictionary.

We can add a new key-value pair in a dictionary using the update() method. If the key already exists in the dictionary, then the value of the key is updated. If the key does not exist in the dictionary, then a new key-value pair is added to the dictionary.

dict_update_iterable.py
data = {'a': 1, 'b': 2, 'c': 3, 'd': 4}
data.update([('e', 5), ('f', 6)])
print(data)

Output:

command
C:\Users\username>python dict_update_iterable.py
{'a': 1, 'b': 2, 'c': 3, 'd': 4, 'e': 5, 'f': 6}

In this example, we declare a dictionary and assign it to the variable data. We then add a new key-value pair to the dictionary using the update() method. The output shows that the new key-value pair is added to the dictionary.

We can add a new key-value pair in a dictionary using the update() method. If the key already exists in the dictionary, then the value of the key is updated. If the key does not exist in the dictionary, then a new key-value pair is added to the dictionary.

dict_update_keyword.py
data = {'a': 1, 'b': 2, 'c': 3, 'd': 4}
data.update(e=5, f=6)
print(data)

Output:

command
C:\Users\username>python dict_update_keyword.py
{'a': 1, 'b': 2, 'c': 3, 'd': 4, 'e': 5, 'f': 6}

In this example, we declare a dictionary and assign it to the variable data. We then add a new key-value pair to the dictionary using the update() method. The output shows that the new key-value pair is added to the dictionary.

We can add a new key-value pair in a dictionary using the update() method. If the key already exists in the dictionary, then the value of the key is updated. If the key does not exist in the dictionary, then a new key-value pair is added to the dictionary. We are using the unpacking operator(**) to add a new key-value pair in a dictionary.

dict_update_unpacking.py
data = {'a': 1, 'b': 2, 'c': 3, 'd': 4}
data2 = {'e': 5, 'f': 6}
data.update(**data2)
print(data)

Output:

command
C:\Users\username>python dict_update_unpacking.py
{'a': 1, 'b': 2, 'c': 3, 'd': 4, 'e': 5, 'f': 6}

In this example, we declare a dictionary and assign it to the variable data. We then add a new key-value pair to the dictionary using the update() method. The output shows that the new key-value pair is added to the dictionary.

Create a new dictionary using the Unpacking operator (**) and update() method

Section titled “Create a new dictionary using the Unpacking operator (**) and update() method”

We can add a new key-value pair in a dictionary using the update() method. If the key already exists in the dictionary, then the value of the key is updated. If the key does not exist in the dictionary, then a new key-value pair is added to the dictionary. We are using the unpacking operator(**) to add a new key-value pair in a dictionary.

dict_update_unpacking_new.py
data = {'a': 1, 'b': 2, 'c': 3, 'd': 4}
data2 = {'e': 5, 'f': 6}
data3 = {**data, **data2}
print(data3)

Output:

command
C:\Users\username>python dict_update_unpacking_new.py
{'a': 1, 'b': 2, 'c': 3, 'd': 4, 'e': 5, 'f': 6}

In this example, we declare a dictionary and assign it to the variable data. We then add a new key-value pair to the dictionary using the update() method. The output shows that the new key-value pair is added to the dictionary.

Add a new key-value pair using setdefault() method

Section titled “Add a new key-value pair using setdefault() method”

We can add a new key-value pair in a dictionary using the setdefault() method. If the key already exists in the dictionary, then the value of the key is updated. If the key does not exist in the dictionary, then a new key-value pair is added to the dictionary.

dict_setdefault.py
data = {'a': 1, 'b': 2, 'c': 3, 'd': 4}
data.setdefault('e', 5)
print(data)

Output:

command
C:\Users\username>python dict_setdefault.py
{'a': 1, 'b': 2, 'c': 3, 'd': 4, 'e': 5}

In this example, we declare a dictionary and assign it to the variable data. We then add a new key-value pair to the dictionary using the setdefault() method. The output shows that the new key-value pair is added to the dictionary.

We can add a new key-value pair in a dictionary using the Union operator(|). If the key already exists in the dictionary, then the value of the key is updated. If the key does not exist in the dictionary, then a new key-value pair is added to the dictionary. We are using the union operator(|) to add a new key-value pair in a dictionary.

dict_setdefault_union.py
data = {'a': 1, 'b': 2, 'c': 3, 'd': 4}
data2 = {'e': 5, 'f': 6}
data3 = data | data2
print(data3)

Output:

command
C:\Users\username>python dict_setdefault_union.py
{'a': 1, 'b': 2, 'c': 3, 'd': 4, 'e': 5, 'f': 6}

In this example, we declare a dictionary and assign it to the variable data. We are going to add a new key-value pair to the dictionary using the union operator(|). We can also merge two dictionaries using the union operator(|).

We can add a new key-value pair in a dictionary using the Union Equal operator(|=). If the key already exists in the dictionary, then the value of the key is updated. If the key does not exist in the dictionary, then a new key-value pair is added to the dictionary. We are using the union equal operator(|=) to add a new key-value pair in a dictionary.

dict_setdefault_union_equal.py
data = {'a': 1, 'b': 2, 'c': 3, 'd': 4}
data2 = {'e': 5, 'f': 6}
data |= data2
print(data)

Output:

command
C:\Users\username>python dict_setdefault_union_equal.py
{'a': 1, 'b': 2, 'c': 3, 'd': 4, 'e': 5, 'f': 6}

In this example, we declare a dictionary and assign it to the variable data. We are going to add a new key-value pair to the dictionary using the union equal operator(|=). We can also merge two dictionaries using the union equal operator(|=).

In Python, we can remove items from a dictionary using the following ways.

Remove a key-value pair using pop() method

Section titled “Remove a key-value pair using pop() method”

We can remove a key-value pair from a dictionary using the pop() method. The pop() method removes the key-value pair from the dictionary and returns the value of the key. If the key does not exist in the dictionary, then the pop() method raises a KeyError exception.

dict_pop.py
data = {'a': 1, 'b': 2, 'c': 3, 'd': 4}
value = data.pop('a')
print(value)
print(data)

Output:

command
C:\Users\username>python dict_pop.py
1
{'b': 2, 'c': 3, 'd': 4}

In this example, we declare a dictionary and assign it to the variable data. We then remove a key-value pair from the dictionary using the pop() method. The output shows that the key-value pair is removed from the dictionary.

Removing a key-value pair using pop() method with default value

Section titled “Removing a key-value pair using pop() method with default value”

We can remove a key-value pair from a dictionary using the pop() method. The pop() method removes the key-value pair from the dictionary and returns the value of the key. If the key does not exist in the dictionary, then the pop() method returns the default value.

dict_pop_default.py
data = {'a': 1, 'b': 2, 'c': 3, 'd': 4}
value = data.pop('e', 5)
print(value)
print(data)

Output:

command
C:\Users\username>python dict_pop_default.py
5
{'a': 1, 'b': 2, 'c': 3, 'd': 4}

In this example, we declare a dictionary and assign it to the variable data. We then remove a key-value pair from the dictionary using the pop() method. The output shows that the key-value pair is removed from the dictionary.

Removing a key-value pair where the key does not exist using pop() method

Section titled “Removing a key-value pair where the key does not exist using pop() method”

We can remove a key-value pair from a dictionary using the pop() method. The pop() method removes the key-value pair from the dictionary and returns the value of the key. If the key does not exist in the dictionary, then the pop() method raises a KeyError exception.

dict_pop_keyerror.py
data = {'a': 1, 'b': 2, 'c': 3, 'd': 4}
value = data.pop('e')
print(value)
print(data)

Output:

command
C:\Users\username>python dict_pop_keyerror.py
Traceback (most recent call last):
  File "dict_pop_keyerror.py", line 2, in <module>
    value = data.pop('e')
KeyError: 'e'

In this example, we declare a dictionary and assign it to the variable data. We then remove a key-value pair from the dictionary using the pop() method. The output shows that the key-value pair is removed from the dictionary.

Remove a key-value pair using popitem() method

Section titled “Remove a key-value pair using popitem() method”

We can remove a key-value pair from a dictionary using the popitem() method. The popitem() method removes the key-value pair from the dictionary and returns the key-value pair as a tuple. If the dictionary is empty, then the popitem() method raises a KeyError exception.

dict_popitem.py
data = {'a': 1, 'b': 2, 'c': 3, 'd': 4}
key, value = data.popitem()
print(key, value)
print(data)

Output:

command
C:\Users\username>python dict_popitem.py
d 4
{'a': 1, 'b': 2, 'c': 3}

In this example, we declare a dictionary and assign it to the variable data. We then remove a key-value pair from the dictionary using the popitem() method. The output shows that the key-value pair is removed from the dictionary.

We can remove a key-value pair from a dictionary using the del keyword. The del keyword removes the key-value pair from the dictionary. If the key does not exist in the dictionary, then the del keyword raises a KeyError exception.

dict_del.py
data = {'a': 1, 'b': 2, 'c': 3, 'd': 4}
del data['a']
print(data)

Output:

command
C:\Users\username>python dict_del.py
{'b': 2, 'c': 3, 'd': 4}

In this example, we declare a dictionary and assign it to the variable data. We then remove a key-value pair from the dictionary using the del keyword. The output shows that the key-value pair is removed from the dictionary.

We can remove all items from a dictionary using the clear() method. The clear() method removes all items from the dictionary.

dict_clear.py
data = {'a': 1, 'b': 2, 'c': 3, 'd': 4}
data.clear()
print(data)

Output:

command
C:\Users\username>python dict_clear.py
{}

In this example, we declare a dictionary and assign it to the variable data. We then remove all items from the dictionary using the clear() method. The output shows that all items are removed from the dictionary.

We can delete a dictionary using the del keyword. The del keyword deletes the dictionary.

dict_del_dict.py
data = {'a': 1, 'b': 2, 'c': 3, 'd': 4}
del data
print(data)

Output:

command
C:\Users\username>python dict_del_dict.py
Traceback (most recent call last):
  File "dict_del_dict.py", line 3, in <module>
    print(data)
NameError: name 'data' is not defined

In this example, we declare a dictionary and assign it to the variable data. We then delete the dictionary using the del keyword. The output shows that the dictionary is deleted.

In this tutorial, we learned how to add and remove items in a dictionary. We learned how to add a new key-value pair in a dictionary using the [] operator. We learned how to add a new key-value pair in a dictionary using the update() method. We learned how to add a new key-value pair in a dictionary using the setdefault() method. We learned how to add a new key-value pair in a dictionary using the union operator(|). We learned how to add a new key-value pair in a dictionary using the union equal operator(|=). We learned how to remove a key-value pair from a dictionary using the pop() method. We learned how to remove a key-value pair from a dictionary using the popitem() method. We learned how to remove a key-value pair from a dictionary using the del keyword. We learned how to remove all items from a dictionary using the clear() method. We learned how to delete a dictionary using the del keyword. For more information, visit the official Python documentation. For more tutorials, visit our Python Central Hub.


diagram five ways to take something out of a dictionary mermaid
They differ in what they return, what they do when the key is absent, and whether you choose the key at all. Two raise on a miss, two accept a default, and popitem chooses for you -- taking the most recently inserted pair, because a dict remembers insertion order.
sketch Adding and removing, and who wins a collision p5.js
Insertion order is remembered, which is what makes popitem take the newest pair rather than an arbitrary one. On the adding side, both update and the merge operator let the RIGHT-hand value win -- so merging defaults with overrides works only if the overrides are on the right.
pch.quizTag pch.quizDefaultTitle
  1. What does `d.popitem()` remove?

    pch.quizShowAnswer

    C — The LAST inserted pair — Verified: from `{'b':2,'c':3}` it returned `('c', 3)`. Dicts remember insertion order, so `popitem` is LIFO — which is what makes a dict usable as a stack of pending work.

  2. `{'a': 1} | {'a': 9}` gives what?

    pch.quizShowAnswer

    B — `{'a': 9}` — The right-hand side wins. So `defaults | overrides` is the order you want — reversing it silently discards your overrides.

  3. Which removal never raises?

    pch.quizShowAnswer

    C — `d.pop(k, default)` — A default makes the miss ordinary. `del` and `pop(k)` raise `KeyError`; `popitem()` raises only when the dict is empty.

  4. What does `d.update(other)` return?

    pch.quizShowAnswer

    B — `None` — it mutates in place — Same convention as `list.sort()` and `set.update()`. If you want a new dict, use `d | other`, which returns one and leaves both operands alone.

Try it: Add and Remove Dictionary Exercises

Section titled “Try it: Add and Remove Dictionary Exercises”

pch.coffeeTagline

pch.coffeeCta

pch.feedbackHeading

pch.feedbackSubheading