Replace values in a dictionary in Python

avatar

Borislav Hadzhiev

Last updated: Sep 17, 2022

banner

Photo from Unsplash

Replace values in a dictionary in Python #

Use the dict.update() method to replace values in a dictionary, e.g. my_dict.update({'key': 'new value'}). The dict.update() method updates the dictionary with the key-value pairs from the provided value.

main.py
my_dict = { 'name': 'default', 'site': 'default', 'id': 1, 'topic': 'Python' } my_dict.update( {'name': 'borislav', 'site': 'bobbyhadz.com'} ) # 👇️ {'name': 'borislav', 'site': 'bobbyhadz.com', 'id': 1, 'topic': 'Python'} print(my_dict)

We used the dict.update method to replace values in a dictionary.

The dict.update method updates the dictionary with the key-value pairs from the provided value.

The method overrides the dictionary's existing keys and returns None.

The dict.update() method can either be called with another dictionary or an iterable of key-value pairs (e.g. a list of tuples with 2 elements each).

You can also pass keyword arguments to the dict.update() method.

main.py
my_dict = { 'name': 'default', 'site': 'default', 'id': 1, 'topic': 'Python' } my_dict.update( [ ('name', 'borislav'), ('site', 'bobbyhadz.com') ] ) # 👇️ {'name': 'borislav', 'site': 'bobbyhadz.com', 'id': 1, 'topic': 'Python'} print(my_dict)

Alternatively, you can use the dictionary unpacking ** operator.

Replace values in a dictionary using dictionary unpacking #

To replace values in a dictionary:

  1. Use the dictionary unpacking operator to unpack the key-value pairs into a new dictionary.
  2. Specify the keys with the updated values.
  3. The new values will override the values of the existing keys.
main.py
my_dict = { 'name': 'default', 'site': 'default', 'id': 1, 'topic': 'Python' } my_dict = { **my_dict, 'name': 'borislav', 'site': 'bobbyhadz.com' } # 👇️ {'name': 'borislav', 'site': 'bobbyhadz.com', 'id': 1, 'topic': 'Python'} print(my_dict)

We used the dictionary unpacking ** operator to unpack the key-value pairs of the dictionary into a new dictionary.

The name and site keys override the values of the existing keys with the same names.

Alternatively, you can use a for loop.

Replace values in a dictionary using a for loop #

To replace values in a dictionary:

  1. Use a for loop to iterate over the dictionary's items.
  2. Check if each value should be updated.
  3. Replace the matching values.
main.py
my_dict = { 'name': 'default', 'site': 'default', 'id': 1, 'topic': 'Python' } for key, value in my_dict.items(): if value == 'default': if key == 'name': my_dict[key] = 'borislav' elif key == 'site': my_dict[key] = 'bobbyhadz.com' # 👇️ {'name': 'borislav', 'site': 'bobbyhadz.com', 'id': 1, 'topic': 'Python'} print(my_dict)

The dict.items method returns a new view of the dictionary's items ((key, value) pairs).

main.py
my_dict = { 'name': 'default', 'site': 'default', 'id': 1, 'topic': 'Python' } # 👇️ dict_items([('name', 'default'), ('site', 'default'), ('id', 1), ('topic', 'Python')]) print(my_dict.items())

On each iteration, we check if the current value should be replaced and replace the matching values.

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.