Python - Concat Dictionary


Python - Concat Dictionary

In this tutorial, you will learn how to concat a dictionary to other dictionary in Python, with examples.

There are many ways in which we can concatenate a dictionary to another. Some of them are:

  • Using dictionary update() method.
  • Using dictionary unpacking operator.
  • Using a For loop.

Let us go through each of these approaches to concatenate dictionary, with examples.

1. Concatenate Dictionary using update() method in Python

The syntax to concat a dictionary dict_2 to a dictionary dict_1 using dictionary update() method is

dict_1.update(dict_2)

Example

In the following program, we take two dictionaries: dict1 and dict2. We shall concat these two dictionaries using dictionary update() method.

Python Program

dict_1 = {
    'foo':12,
    'bar':14,
    'moo':16
}

dict_2 = {
    'gee': 18,
    'kee': 20
}

# Concatenate dictionary dict_1 with dict_2
dict_1.update(dict_2)
print(dict_1)

Output

{'foo': 12, 'bar': 14, 'moo': 16, 'gee': 18, 'kee': 20}

The first dictionary dict_1 is concatenated with the second dictionary dict_2, and the original dictionary is modified.

Please note that if there are duplicate keys across dictionaries, then the value from the second dictionary is considered in the output dictionary.

2. Concatenate Dictionary using unpacking operator in Python

The syntax to unpack two dictionaries: dict_1 and dict_2 to concatenate them into a new dictionary using dictionary unpacking operator ** is

{**dict_1, **dict_2}

The above expression returns a new dictionary with the items in dictionary dict_2 concatenated to items in the dictionary dict_1.

Example

In the following program, we take two dictionaries: dict1 and dict2. We shall concat these two dictionaries using dictionary unpacking operator.

Python Program

dict_1 = {
    'foo':12,
    'bar':14,
    'moo':16
}

dict_2 = {
    'gee': 18,
    'kee': 20
}

# Concatenate dictionaries dict_1 with dict_2
dict_output = {**dict_1, **dict_2}
print(dict_output)

Output

{'foo': 12, 'bar': 14, 'moo': 16, 'gee': 18, 'kee': 20}

3. Concatenate Dictionary using For loop in Python

To concatenate dictionary to another dictionary using a Python For loop, iterate over the second dictionary, and add items from this second dictionary to the first dictionary.

for key, value in dict_2.items():
    dict_1[key] = value

The above code modifies the dictionary dict_1, concatenated with the items from the dictionary dict_2. You may make a copy of the dictionary dict_1, if you would like to preserve the original dictionaries.

Example

In the following program, we take two dictionaries: dict1 and dict2 and concatenate them using For loop.

Python Program

dict_1 = {
    'foo':12,
    'bar':14,
    'moo':16
}

dict_2 = {
    'gee': 18,
    'kee': 20
}

# Concatenate dictionaries dict_1 with dict_2
for key, value in dict_2.items():
    dict_1[key] = value

print(dict_1)

Output

{'foo': 12, 'bar': 14, 'moo': 16, 'gee': 18, 'kee': 20}

Summary

In this tutorial, we learned how to concatenate a dictionary with another by using dictionary update() method, by using dictionary unpacking operator, or by using a For loop, with examples.