Adding elements to a dictionary and joining dictionaries in Python

Money and Business

This section explains how to add new elements to a dictionary (dict type object) or update the value of an existing element in Python. It is also possible to concatenate (join, merge) multiple dictionaries.

  • Add and update elements to the dictionary by specifying keys.
  • Concatenation (merging) of multiple dictionaries:update(),| operator,|= operator
  • Add or update multiple elements:update(),|= operator

Add and update elements to the dictionary by specifying keys.

You can add/update dictionary elements in the following way.

Dictionary object [key] = value

When a non-existent key is specified, a new element is added, and when an existing key is specified, the existing value 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}

If you do not want to update the value of a key that exists, use the setdefault() method.

Concatenate (merge) multiple dictionaries: update(), | operator, |= operator

update()

If another dictionary object is specified as an argument to the dictionary object's method update(), all its elements will be added.

If the key overlaps with an existing key, it will be overwritten with the value of the dictionary specified in the argument.

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}

It is an error to specify multiple dictionaries in the update() argument.

d1 = {'k1': 1, 'k2': 2}
d2 = {'k3': 3, 'k4': 4}
d3 = {'k5': 5, 'k6': 6}

# d1.update(d2, d3)
# TypeError: update expected at most 1 arguments, got 2

As explained later, update() can add new elements as keyword arguments (key=value), so just add ** to the dictionary and expand each element as a keyword argument and pass it.

d1.update(**d2, **d3)
print(d1)
# {'k1': 1, 'k2': 2, 'k3': 3, 'k4': 4, 'k5': 5, 'k6': 6}

As in the previous examples, using update() will update the original dictionary object.

If you want to generate a new dictionary by merging multiple dictionaries, use {**d1, **d2} (from Python 3.5) or dict(**d1, **d2).

In Python 3.9 and later, it is also possible to create a new dictionary using the| operator described next.

| operator, |= operator (Python 3.9 and later)

Since Python 3.9, it is possible to merge two dictionaries using the | operator. When two dictionaries have the same key, the value on the right has priority.

d1 = {'k1': 1, 'k2': 2}
d2 = {'k1': 100, 'k3': 3, 'k4': 4}

print(d1 | d2)
# {'k1': 100, 'k2': 2, 'k3': 3, 'k4': 4}

print(d2 | d1)
# {'k1': 1, 'k3': 3, 'k4': 4, 'k2': 2}

|It is also possible to combine multiple dictionaries by using a series of operators.

d1 = {'k1': 1, 'k2': 2}
d2 = {'k3': 3, 'k4': 4}
d3 = {'k5': 5, 'k6': 6}

print(d1 | d2 | d3)
# {'k1': 1, 'k2': 2, 'k3': 3, 'k4': 4, 'k5': 5, 'k6': 6}

+As with update(), the object on the left side is updated.

d1 = {'k1': 1, 'k2': 2}
d2 = {'k1': 100, 'k3': 3, 'k4': 4}

d1 |= d2
print(d1)
# {'k1': 100, 'k2': 2, 'k3': 3, 'k4': 4}

Add or update multiple elements: update(), |= operator

update()

When the keyword argument key=value is specified in the update() method, the key key and value value will be added. If the key overlaps with an existing key, it will be 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}

It is also possible to specify a list of (key, value) as an argument to the update() method. If the key overlaps with an existing key, it will be overwritten with the value specified as 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 combination with the zip() function, elements can be added 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}

|= operator (Python 3.9 and later)

With the |= operator, a list of (key, value) can be specified on the right side.

d = {'k1': 1, 'k2': 2}

d |= [('k1', 100), ('k3', 3), ('k4', 4)]
print(d)
# {'k1': 100, 'k2': 2, 'k3': 3, 'k4': 4}

Note that specifying a list with the | operator will result in an error. Only dictionary-to-dictionary operations are supported.

# print(d | [('k1', 100), ('k3', 3), ('k4', 4)])
# TypeError: unsupported operand type(s) for |: 'dict' and 'list'
Copied title and URL