
Table of Contents(目次)
How to Add and Update Elements in Python dict (Behavior of "dict[key] = value")
You can overwrite a value or add a new key to a dict by writing dict[key] = value
. The behavior changes depending on whether the key already exists in the dict.
- When the key exists... the value is overwritten
- When the key does not exist...
key: value
is added
Run the following code to check the behavior of dict[key] = value
in each case.
my_profile = {'name': 'Nico'}
my_profile['name'] = 'Casey'
my_profile['friends'] = ['Emma', 'Jack']
print(my_profile) # {'name': 'Casey', 'friends': ['Emma', 'Jack']}
When you run the code above and check the output, ['name']
in the variable "my_profile" already exists, so the value is overwritten with "Casey". Since ['friends']
does not exist, 'friends': ['Emma', 'Jack']
is added.
The merge (|) Operator
Available in Python 3.9 and later
* This feature is available in Python 3.9 and later.
Lists and tuples combine elements using the +
operator, but dicts can combine elements using |
(vertical bar).
my_profile = {'name': 'Nico', 'age': 10}
add_profile = {'friends': ['Emma', 'Jack']}
new_profile = my_profile | add_profile
print(new_profile) # {'name': 'Nico', 'age': 10, 'friends': ['Emma', 'Jack']}
print(my_profile) # {'name': 'Nico', 'age': 10}
print(add_profile) # {'friends': ['Emma', 'Jack']}
When dicts are combined with |
, a new dict is created and assigned to the variable "new_profile". The contents of "my_profile" and "add_profile" remain unchanged.
If both "my_profile" and "add_profile" contain the same key, the value from the dict on the right side of |
takes precedence. In the following code, the value of ['age']
in "add_profile" is used, so new_profile['age']
becomes 99
.
my_profile = {'name': 'Nico', 'age': 10}
add_profile = {'friends': ['Emma', 'Jack'], 'age':99}
new_profile = my_profile | add_profile
print(new_profile) # {'name': 'Nico', 'age': 99, 'friends': ['Emma', 'Jack']}
print(my_profile) # {'name': 'Nico', 'age': 10}
print(add_profile) # {'friends': ['Emma', 'Jack'], 'age': 99}
Combining dicts in Versions Prior to Python 3.9 (update() Method)
* For versions earlier than Python 3.9.
If you want to combine the elements of dicts, use the update()
method.
my_profile = {'name': 'Nico', 'age': 10}
add_profile = {'friends': ['Emma', 'Jack'], 'age':99}
my_profile.update(add_profile)
print(my_profile) # {'name': 'Nico', 'age': 99, 'friends': ['Emma', 'Jack']}
print(add_profile) # {'friends': ['Emma', 'Jack'], 'age': 99}
Note that the update()
method modifies the contents of the dictionary that called the method. If you want to use update()
but also keep the original data, first use the copy()
method to assign a copy to another variable, and then use update()
. (The copy()
method creates a shallow copy.)
my_profile = {'name': 'Nico', 'age': 10}
add_profile = {'friends': ['Emma', 'Jack'], 'age':99}
new_profile = my_profile.copy()
new_profile.update(add_profile)
print(new_profile) # {'name': 'Nico', 'age': 99, 'friends': ['Emma', 'Jack']}
print(my_profile) # {'name': 'Nico', 'age': 10}
print(add_profile) # {'friends': ['Emma', 'Jack'], 'age': 99}