Table of Contents
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,
1 2 3 4 5 6 |
d = {} d['k1'] = 10 d['k2'] = 15 print(d) |
Output:
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.
Further reading:
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.
1 2 3 4 5 |
d = {} d.update({'k1': 10, 'k2': 15}) print(d) |
Output:
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.
1 2 3 4 5 6 |
l1 = ['k1','k2'] l2 = [10,15] d = {k:v for k,v in zip(l1,l2)} print(d) |
Output:
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,
1 2 3 4 5 6 |
d1 = {'k1':10} d2 = {'k2':15} d1.update(d2) print(d1) |
Output:
In the above example,
- We define two dictionaries,
d1
andd2
. - We append the
d2
dictionary to thed1
dictionary. - The
d1
dictionary now contains elements of thed2
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.
1 2 3 4 5 6 |
d1 = {'k1':10} d2 = {'k2':15} d3 = {**d1, **d2} print(d3) |
Output:
In the above example,
- The
**
operator unpacks the elements of thed1
andd2
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,
1 2 3 4 5 6 7 |
from itertools import chain d1 = {'k1':10} d2 = {'k2':15} d3 = dict(chain(d1.items(),d2.items())) print(d3) |
Output:
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 thedict
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,
1 2 3 4 5 6 |
d1 = {'k1':10} d2 = {'k2':15} d3 = d1 | d2 print(d3) |
Output:
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.
1 2 3 4 |
d1 = {'k1':{'k3':20}, 'k2':15} print(d1) |
Output:
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.
1 2 3 4 5 |
d1 = {'k1':{'k3':20}, 'k2':15} d1['k4'] = {'k5':25} print(d1) |
Output:
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.