Add and Update an Item in a Dictionary in Python
This article explains how to add an item (key-value pair) to a dictionary (dict
) or update the value of an existing item in Python.
See the following articles to learn how to add a dictionary to a dictionary (i.e., merge dictionaries), remove an item from a dictionary, and change a key name.
- Merge dictionaries in Python
- Remove an item from a dictionary in Python (clear, pop, popitem, del)
- Change a key name in a dictionary in Python
Add or update a single item in a dictionary
You can add an item to a dictionary or update the value of an existing item as follows.
dict_object[key] = value
If a non-existent key is specified, a new item is added; if an existing key is specified, the value of that item is updated (overwritten).
d = {'k1': 1, 'k2': 2}
d['k3'] = 3
print(d)
# {'k1': 1, 'k2': 2, 'k3': 3}
d['k1'] = 100
print(d)
# {'k1': 100, 'k2': 2, 'k3': 3}
To avoid updating the value for an existing key, use the setdefault()
method. See the following article for details.
Add or update multiple items in a dictionary: update()
You can add or update multiple items at once using the update()
method.
Specify keyword arguments
If the keyword argument (key=value
) is specified for update()
, the item with that key and value is added. If the key already exists, it is overwritten with the value specified in the argument.
d = {'k1': 1, 'k2': 2}
d.update(k1=100, k3=3, k4=4)
print(d)
# {'k1': 100, 'k2': 2, 'k3': 3, 'k4': 4}
An error is raised if the same key is specified multiple times.
# d.update(k3=3, k3=300)
# SyntaxError: keyword argument repeated: k3
In this case, keys must be valid identifiers in Python. They cannot start with a number or contain symbols other than _
.
# d.update(k-3=3)
# SyntaxError: expression cannot contain assignment, perhaps you meant "=="?
# d.update(4=400)
# SyntaxError: expression cannot contain assignment, perhaps you meant "=="?
In other approaches, values that are invalid as identifiers can be used as keys.
d = {'k1': 1, 'k2': 2}
d['k-3'] = 3
d[4] = 400
print(d)
# {'k1': 1, 'k2': 2, 'k-3': 3, 4: 400}
Specify an iterable of key-value pairs
You can pass a list of (key, value)
pairs to update()
. If a key in the list duplicates an existing key, it is overwritten with the value specified in the argument.
d = {'k1': 1, 'k2': 2}
d.update([('k1', 100), ('k3', 3), ('k4', 4)])
print(d)
# {'k1': 100, 'k2': 2, 'k3': 3, 'k4': 4}
In the above example, a list of tuples was specified. However, any iterable containing key-value pairs (two-element iterables) is acceptable. This could include a tuple of lists, such as ([key1, value1], [key2, value2], ...)
, or other iterable structures.
You can use zip()
to add items by pairing elements from a list of keys and a list of values.
d = {'k1': 1, 'k2': 2}
keys = ['k1', 'k3', 'k4']
values = [100, 3, 4]
d.update(zip(keys, values))
print(d)
# {'k1': 100, 'k2': 2, 'k3': 3, 'k4': 4}
When using an iterable of key-value pairs, duplicate keys are acceptable. The value corresponding to the later occurrence of a key will overwrite the earlier one.
d = {'k1': 1, 'k2': 2}
d.update([('k3', 3), ('k3', 300)])
print(d)
# {'k1': 1, 'k2': 2, 'k3': 300}
d = {'k1': 1, 'k2': 2}
keys = ['k3', 'k3']
values = [3, 300]
d.update(zip(keys, values))
print(d)
# {'k1': 1, 'k2': 2, 'k3': 300}
Specify other dictionaries
You can specify another dictionary as an argument to update()
to add all its items.
d1 = {'k1': 1, 'k2': 2}
d2 = {'k1': 100, 'k3': 3, 'k4': 4}
d1.update(d2)
print(d1)
# {'k1': 100, 'k2': 2, 'k3': 3, 'k4': 4}
Passing multiple dictionaries directly to update()
will result in an error. You can prefix dictionaries with **
and pass each element as a keyword argument.
d1 = {'k1': 1, 'k2': 2}
d2 = {'k1': 100, 'k3': 3, 'k4': 4}
d3 = {'k5': 5, 'k6': 6}
# d1.update(d2, d3)
# TypeError: update expected at most 1 arguments, got 2
d1.update(**d2, **d3)
print(d1)
# {'k1': 100, 'k2': 2, 'k3': 3, 'k4': 4, 'k5': 5, 'k6': 6}
When using **
, as shown in the above example, duplicate keys between the caller's dictionary and the dictionary specified in the argument are not a problem. However, if the same keys are found across multiple dictionaries specified in the argument, this will result in an error.
d1 = {'k1': 1, 'k2': 2}
d2 = {'k1': 100, 'k3': 3, 'k4': 4}
d3 = {'k5': 5, 'k6': 6}
# d3.update(**d1, **d2)
# TypeError: dict.update() got multiple values for keyword argument 'k1'
For more details on merging dictionaries, refer to the following article.