note.nkmk.me

Merge multiple dictionaries and add items to a dictionary in Python

Modified: | Tags: Python, Dictionary

This article explains how to add a new item to a dictionary (dict) or update the value of an existing item in Python. It is also possible to merge multiple dictionaries.

See the following articles on how to remove an item from a dictionary, check the existence of a key, and change the key.

Sponsored Link

Add or update an item in the dictionary by specifying a key

You can add an item to the dictionary or update the value of an existing item as follows.

dict_object[key] = value

A new item is added if a non-existent key is specified, and the value of the existing item is updated (overwritten) if an existing key is specified.

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.

Merge multiple dictionaries: update(), |, |=

update()

By specifying another dict as an argument of the update() method, all its items are added.

If the key overlaps with an existing key, it is overwritten with the value of dict 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}

An error occurs when more than one dictionary is specified as an argument for the update() method.

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

As described later, update() can add new items with keyword arguments (key=value), so you can unpack and pass each item with **.

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

In this case, having duplicate keys between the calling dictionary and the dictionary specified in the argument is allowed, as shown in the above example. However, an error is raised if there are duplicate keys among the dictionaries specified in the argument.

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'

{} (Python 3.5 or later), dict()

With update(), the original dictionary is updated.

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

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

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

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

An error occurs when using dict(**d1, **d2) if there are duplicate keys in the dictionaries specified as arguments.

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

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

# print(dict(**d1, **d2))
# TypeError: dict() got multiple values for keyword argument 'k1'

See the following articles for details on how to create a dictionary.

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

| and |= operators (Python 3.9 or later)

Starting from Python 3.9, the | operator can be used to merge two dictionaries. If they have the same key, it is overwritten by the value on the right.

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

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

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

You can combine multiple dictionaries.

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

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

Like += for +, |= for | is also provided. As with update(), the object on the left 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}
Sponsored Link

Add or update multiple items in the dictionary: update(), |=

update()

If the keyword argument key=value is specified to the update() method, the item with its key and value is added. If the key overlaps with an existing one, it is overwritten with the value specified as an 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 one, it is overwritten with the value specified as an argument.

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

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

You can use zip() to add items 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}

See the following article about zip().

For keyword arguments, an error is raised if the same key is specified. In the case of (key, value) lists and zip(), duplicate keys are acceptable. The later value overwrites the previous one.

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

# d.update(k3=3, k3=300)
# SyntaxError: keyword argument repeated: k3

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}

|= operator(Python 3.9 or later)

The |= operator allows a list of (key, value) to 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}

The | operator only supports operations between dictionaries. You cannot specify a list.

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

Related Categories

Related Articles