Add Dictionary to Dictionary in Python

In this post, we will see how to add dictionary to dictionary in Python

A dictionary in Python is used to store elements in the form of key-value pairs. The keys can access the corresponding values and should be unique. With Python 3.6, dictionaries also preserve their order. This was not the case in the previous versions.

We can perform various operations to update a dictionary. We can add, append dictionary to dictionary in Python. A dictionary can take other dictionaries as values as well. This is called a nested dictionary. This tutorial will demonstrate different methods to perform such operations on dictionaries in Python.

Add dictionary to empty dictionary in Python

Let us first discuss how to handle an empty dictionary in Python. We will add key-value pairs to an empty dictionary in the methods shown below.

Using the [] operator to add dictionary to empty dictionary in Python

The [] operator can add key-value pairs to a dictionary. First, we need to define an empty dictionary either by using the curly brackets {} or the dict() constructor.

We will specify the key in the [] brackets and assign it with a value using the = operator.

For example,

Output:

{‘k1’: 10, ‘k2’: 15}

In the above example,

  • We create an empty dictionary using curly brackets.
  • We add two key-pair elements individually using the [] brackets.
  • The dictionary is displayed.

Using the update() function to add dictionary to empty dictionary in Python

The update() function adds the key-value pairs to an existing dictionary. We can directly add dictionaries as well. The original dictionary will be modified while using this function.

See the code below.

Output:

{‘k1’: 10, ‘k2’: 15}

Using the dictionary comprehension to add dictionary to empty dictionary in Python

Similar to a list comprehension, we can use dictionary comprehension to create a dictionary with elements.

We will use a for loop to iterate over two lists to add the key-value pairs to the dictionary.

See the code below.

Output:

{‘k1’: 10, ‘k2’: 15}

In the above example,

  • We create two lists, one for the keys and the second for the values of the keys.
  • We use dictionary comprehension to iterate over the two lists and add them to the dictionary.
  • The zip() function combines the elements of the two lists in a single object.

Append dictionary to dictionary in Python

In this section, we will discuss how to append a dictionary to dictionary in Python. This means that we will essentially combine two dictionaries.

Using the update() function to append dictionary to dictionary in Python

As discussed earlier, the update() function updates a given dictionary by combining elements to it. We can append dictionary to dictionary using this function.

For example,

Output:

{‘k1’: 10, ‘k2’: 15}

In the above example,

  • We define two dictionaries, d1 and d2.
  • We append the d2 dictionary to the d1 dictionary.
  • The d1 dictionary now contains elements of the d2 dictionary as well.

Using the unpack operator (**) to append dictionary to dictionary in Python

In Python 3.5, we can use the ** operator to unpack elements from a dictionary. To append dictionary to dictionary in Python, we will unpack the elements from both the dictionaries and combine them to a new dictionary.

See the code below.

Output:

{‘k1’: 10, ‘k2’: 15}

In the above example,

  • The ** operator unpacks the elements of the d1 and d2 dictionaries.
  • These unpacked values are stored in the d3 dictionary.
  • The d3 dictionary now contains the elements of both the dictionaries.

Using the itertools.chain object to append dictionary to dictionary in Python

We can use the itertools module to work on complex operations with iterables.

We can map different dictionaries into one object using the chain object from the itertools module. We then pass this object to the dict() constructor to get the final result in the dictionary.

For example,

Output:

{‘k1’: 10, ‘k2’: 15}

In the above example,

  • The items() function returns the elements of the dictionary as tuples in a list.
  • These two lists are combined using the chain object.
  • The chain object is converted to a dictionary using the dict constructor.

Using the | operator to append dictionary to dictionary in Python

The | operator was added in Python 3.9 to work with objects of the dict class. They can merge two dictionaries into one. It was introduced to complement the existing update() function.

See the code below.

For example,

Output:

{‘k1’: 10, ‘k2’: 15}

Add dictionary to nested dictionary in Python

A nested dictionary is a dictionary within another dictionary. Here, the value for the key is another dictionary. We can access elements from the nested dictionary by first accessing the dictionary value from the outer dictionary and then accessing the required value from the nested dictionary.

We will create a nested dictionary in the following example.

Output:

{‘k1’: {‘k3’: 20}, ‘k2’: 15}

We will now discuss some other methods to add dictionary to nested dictionary in Python.

Using the [] operator to add dictionary to nested dictionary in Python

The [] operator can add keys to a given dictionary and its values are provided using the = operator. We can provide the value for a key as another dictionary.

See this clearly in the following code.

Output:

{‘k1’: {‘k3’: 20}, ‘k2’: 15, ‘k4’: {‘k5’: 25}}

Conclusion

In this article, we demonstrated different operations associated with a dictionary. We first discussed how to add dictionary to an empty dictionary. This section demonstrated the use of the [] operator, update() function, and the dictionary comprehension method. We then discussed how to add dictionary to dictionary in Python. Here we also used the update() function. We also used the unpack and | operators in this section. The use of itertools.chain object was also demonstrated. The final section discussed nested dictionaries in Python. We discussed how to add dictionary to nested dictionary in Python.

That’s all about how to add dictionary to dictionary in Python.

Was this post helpful?

Leave a Reply

Your email address will not be published. Required fields are marked *