Last updated: Apr 9, 2024
Reading timeยท5 min
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.
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.
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).dict.update()
methodYou can also pass keyword arguments to the dict.update()
method.
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.
This is a three-step process:
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.
This is a three-step process:
for
loop to iterate over the dictionary's items.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).
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.
You can also use the dictionary merge operator to replace values in a dictionary.
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)
The dictionary merge (|) operator is available starting with Python version 3.9.
You can check your version of Python by running the following command.
python --version
The dictionary merge (|) operator creates a new dictionary.
The is also a dictionary update (|=) operator that is used for assignment.
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.
You can also use a for
loop to replace the values in a dictionary based on
another dictionary.
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)
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.
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.
You can also use a dict comprehension to replace values in a dictionary.
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)
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:
Name | Description |
---|---|
key | The key for which to return the value |
default | The default value to be returned if the provided key is not present in the dictionary (optional) |
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
If a value for the default
parameter is not provided, it defaults to None
,
so the get()
method never raises a KeyError
.
You can learn more about the related topics by checking out the following tutorials: