How to Replace values in a Dictionary in Python

avatar
Borislav Hadzhiev

Last updated: Apr 9, 2024
5 min

banner

# Table of Contents

  1. Replace values in a dictionary in Python
  2. Replace values in a dictionary using dictionary unpacking
  3. Replace values in a dictionary using a for loop
  4. Replace values in a dictionary using the dictionary merge operator
  5. Replace values in a dictionary based on another dictionary
  6. Replace values in a dictionary using a dict comprehension

# Replace values in a dictionary in Python

Use 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.

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)

replace values in dictionary

The code for this article is available on GitHub

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).

# Passing keyword arguments to the dict.update() method

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)

passing keyword argument to the dict update method

The code for this article is available on GitHub

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

# Replace values in a dictionary using dictionary unpacking

This is a three-step process:

  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)

replace values in dictionary using unpacking

The code for this article is available on GitHub

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

This is a three-step process:

  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)

replace values in dictionary using for loop

The code for this article is available on GitHub

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.

# Replace values in a dictionary using the dictionary merge operator

You can also use the dictionary merge operator to replace values in a dictionary.

main.py
my_dict = { 'name': 'default', 'site': 'default', 'id': 1, 'topic': 'Python' } my_dict = my_dict | { 'name': 'bobby hadz', 'site': 'bobbyhadz.com' } # {'name': 'bobby hadz', 'site': 'bobbyhadz.com', # 'id': 1, 'topic': 'Python'} print(my_dict)

replace values in dictionary using dictionary merge operator

The code for this article is available on GitHub

The dictionary merge (|) operator is available starting with Python version 3.9.

You can check your version of Python by running the following command.

shell
python --version

The dictionary merge (|) operator creates a new dictionary.

The is also a dictionary update (|=) operator that is used for assignment.

main.py
my_dict = { 'name': 'default', 'site': 'default', 'id': 1, 'topic': 'Python' } my_dict |= { 'name': 'bobby hadz', 'site': 'bobbyhadz.com' } # {'name': 'bobby hadz', 'site': 'bobbyhadz.com', # 'id': 1, 'topic': 'Python'} print(my_dict)

Make sure your version of Python is 3.9 or more recent to be able to run the code sample.

# Replace values in a dictionary based on another dictionary

You can also use a for loop to replace the values in a dictionary based on another dictionary.

main.py
my_dict = { 'name': 'default', 'site': 'default', 'id': 1, 'topic': 'Python' } another_dict = { 'name': 'bobby hadz', 'site': 'bobbyhadz.com' } for key, value in another_dict.items(): my_dict[key] = value # ๐Ÿ‘‡๏ธ {'name': 'bobby hadz', 'site': 'bobbyhadz.com', # 'id': 1, 'topic': 'Python'} print(my_dict)

replace values in dictionary based on another dictionary

The code for this article is available on GitHub

We used a for loop to iterate over the items of the second dictionary.

On each iteration, we replace the key-value pair of the first dictionary.

You can also check for the existence of the keys in the first dictionary.

main.py
my_dict = { 'name': 'default', 'site': 'default', 'id': 1, 'topic': 'Python' } another_dict = { 'name': 'bobby hadz', 'site': 'bobbyhadz.com', 'abc': 'xyz', 'one': 'two', } for key, value in another_dict.items(): if key in my_dict: my_dict[key] = value # ๐Ÿ‘‡๏ธ {'name': 'bobby hadz', 'site': 'bobbyhadz.com', # 'id': 1, 'topic': 'Python'} print(my_dict)

On each iteration, we use the in operator to check if the current key is contained in the dictionary.

The keys are only replaced if they exist in the first dictionary.

# Replace values in a dictionary using a dict comprehension

You can also use a dict comprehension to replace values in a dictionary.

main.py
my_dict = { 'name': 'default', 'site': 'default', 'id': 1, 'topic': 'Python' } another_dict = { 'name': 'bobby hadz', 'site': 'bobbyhadz.com', 'abc': 'xyz', 'one': 'two', } my_dict = { key: another_dict.get(key, value) for key, value in my_dict.items() } # {'name': 'bobby hadz', 'site': 'bobbyhadz.com', # 'id': 1, 'topic': 'Python'} print(my_dict)
The code for this article is available on GitHub

We used a dict comprehension to iterate over the dictionary's items.

Dict comprehensions are very similar to list comprehensions.

They perform some operation for every key-value pair in the dictionary or select a subset of key-value pairs that meet a condition.

On each iteration, we use the dict.get() method to get the value of the key in the second dictionary.

We specified the current value as a fallback in case the key doesn't exist in the second dictionary.

The dict.get method returns the value for the given key if the key is in the dictionary, otherwise a default value is returned.

The method takes the following 2 parameters:

NameDescription
keyThe key for which to return the value
defaultThe default value to be returned if the provided key is not present in the dictionary (optional)
main.py
another_dict = { 'name': 'bobby hadz', 'site': 'bobbyhadz.com', 'abc': 'xyz', 'one': 'two', } print(another_dict.get('id')) # ๐Ÿ‘‰๏ธ None print(another_dict.get('topic')) # ๐Ÿ‘‰๏ธ None print(another_dict.get('name')) # ๐Ÿ‘‰๏ธ bobby hadz
The code for this article is available on GitHub

If a value for the default parameter is not provided, it defaults to None, so the get() method never raises a KeyError.

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

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.

Copyright ยฉ 2024 Borislav Hadzhiev