How to Add Dictionary to Dictionary in Python?

To store data in an unordered collection and in key-value pairs, the “Dictionary” data type is used in Python. The keys are used to identify the values, which can be of any data type. Dictionaries are easy to read and have a unique key name for each element. This means the dictionary values can be accessed through its keys.

Sometimes in programming, we need to append one dictionary data into another when we want to combine the information they contain. Python provides various methods to append one dictionary to another dictionary.

This post will explain different built functions and operators to add one dictionary to another:

Method 1: Using update() Function

The “update()” function is used in Python to add specific items to the dictionary. In the example given below, the “update()” function is used to add one dictionary to another:

Example 1: Adding Dictionary to Dictionary

To add the dictionary to another dictionary, execute the given below command:

Code:

dict_value = {"Alex": 26, "John": 13, "Lily": 12}
dict_value1 = {"Jack": 46, "David": 33, "Adam": 22}
dict_value.update( dict_value1 )
print(dict_value)

In the above code, the “update()” function accepts a dictionary “dict_value1” as an argument and  appends its values with another dictionary, “dict_value”.

Output:

The above output shows that the two dictionaries are added to each other using the “update()” function.

Example 2: Create New Dictionary by Adding Dictionary to Dictionary

The previous example modified the first dictionary by adding the second dictionary values to it. But if we want to add two dictionaries into a new dictionary, then an empty dictionary can be used in Python.

Code:

dict_value = {"Alex": 26, "John": 13, "Lily": 12}
dict_value1 = {"Jack": 46, "David": 33, "Adam": 22}
empty_dict = {}
empty_dict.update(dict_value)
empty_dict.update(dict_value1)
print(empty_dict)

In the above code, an empty dictionary is initialized in a program. The “update()” function accepts each dictionary as an argument and appends it to a new empty dictionary.

Output:

The above output verified that both dictionaries are appended to the new dictionary.

Method 2: Using for Loop

The loop is used in Python to iterate some parts of code multiple times. The for-loop iteration can also add the dictionary value to another dictionary. Let’s understand it by the following code:

Code:

dict_value = {"Alex": 26, "John": 13, "Lily": 12}
dict_value1 = {"Jack": 46, "David": 33, "Adam": 22}
for key, value in dict_value1.items():
    dict_value[key] = value
print(dict_value)

In the above code, the for loop iterates over the dictionary “dict_value1” and returns the value of each item using the “dict.items()” function. The return values are appended into another dictionary, “dict_value”.

Output:

The above output successfully added two input dictionaries using for loop.

Method 3: Using Unpacking Operator **

The unpacking operator “**” is used to unpack the dictionary values to variable values in Python. The below code is used to add two dictionaries in Python using the “**” unpacking operator:

Code:

dict_value = {"Alex": 26, "John": 13, "Lily": 12}
dict_value1 = {"Jack": 46, "David": 33, "Adam": 22}
output ={**dict_value, **dict_value1}
print(output)

In the above code, the unpacking operator is used along with dictionary variables “dict_value” and “dict_value1” to add the two dictionaries.

Output:

The above output shows that the two dictionaries’ values are appended using the unpacking operator.

Method 4: Using | Operator

The OR “|” operator is also used in Python to add/append two dictionaries. Here is an example of this method:

Code:

dict_value = {"Alex": 26, "John": 13, "Lily": 12}
dict_value1 = {"Jack": 46, "David": 33, "Adam": 22}
output = dict_value | dict_value1
print(output)

In the above code, the “OR” operator is placed between the two dictionaries “dict_value” and “dict_value1”. Consequently, it will add the “dict_value” to “dict_vlaue1”.

Output:

The output shows that the “|” operator successfully added the given dictionaries.

Conclusion

The “update()” function, “for loop”, “unpacking operator (**)”, and “ | Operator” are used in Python to add a dictionary to another dictionary. The “update()” function adds the dictionary to another by modifying the original dictionary or appending it to the new empty dictionary. The unpacking operator and “|” operators are also used to append one dictionary into another very efficiently. This article covered all the methods to add or append the dictionary into another dictionary using practical examples.