. Advertisement .
..3..
. Advertisement .
..4..
A Python dictionary consists of a group of elements. These characters come in the key-value pair form, which is mutable and unordered.
This post discusses some straightforward methods to add dictionary to dictionary in Python.
How To Add Dictionary To Dictionary In Python
Method 1: Use The update() Method
Using the update()
method is one of the most common ways to append a dictionary to another one. With this function, you will insert one dictionary’s key-value pairs to another.
It will overwrite the same key values of existing and new dictionaries. Thus, make sure that no valuable piece of information is accidentally overwritten.
Input:
D1 = {"loginID":"xyz","country":"USA"}
D2 = {"firstname":"justin","lastname":"lambert"}
D1.update(D2)
print(D1)
Output:
{'loginID': 'xyz', 'country': 'USA', 'firstname': 'justin', 'lastname': 'lambert'}
In this example, the dictionary D1 is concatenated into the D2 with the updated()
function. This means the D1 is updated with the D2 dictionary’s values.
The update()
function accepts all key-value pairs as arguments and inserts them to the calling dictionary object.
Method 2: Use The Unpacking Operator **
the unpacking operator **
is an updated version of the former one. It not only appends two Python dictionaries but also stores this combination in the third one.
This operator will not change the original dictionary’s key-value pairs. Note that your Python should be at least version 3.5 for effective operation.
Code:
def merge(D1,D2):
py={**D1,**D2}
return py
D1 = {"loginID":"xyz","country":"USA"}
D2 = {"firstname":"justin","lastname":"lambert"}
D3 =(merge(D1,D2))
print(D3)
Output:
{'loginID': 'xyz', 'country': 'USA', 'firstname': 'justin', 'lastname': 'lambert'}
Method 3: Use The | Operator
This approach can be used in the latest version of Python, Python 3.9. It is the easiest and most convenient to merge two different dictionaries.
Code:
def Merge(D1,D2):
py = D1 | D2
return py
D1 = {"RollNo": "10", "Age":"18"}
D2 = {"Marks": "90", "Grade": "A"}
D3 = Merge(D1, D2)
print(D3)
Output:
{'RollNo': '10', 'Age':'18' ,'Marks': '90', 'Grade': 'A'}
Method 4: Use The collections.ChainMap Container
As its name suggests, the ChainMap function maps numerous dictionaries into one unit. Yet, it requires you to import the unit from the collection modules.
Before combining two dictionaries, You need to create a ChainMap container with the ChainMap()
constructor. At the same time, pass and combine the dictionaries as a set of arguments.
Code:
from collections import ChainMap
D1 = {'w':1,'x':2}
D2 = {'y':3, 'z':4}
D3 = ChainMap(D1,D2)
D3 = {k: v for d in (D1, D2) for (k, v) in d.items()}
print(D3)
Output:
{'w': 1, 'x': 2, 'y': 3, 'z': 4}
Note that this method is ideal for Python 2.7 version.
Method 5: Use Loop
A dictionary’s key-value pairs can be iterated one by one while being inserted into another dictionary. The []
operator can help you complete this task.
Code:
dict_1 = { "Hello": 56,
"at": 23,
"test": 43,
"this": 12 }
dict_2 = { 'where': 4,
'who': 5,
'why': 6,
'this': 20 }
# Add items from dict_2 to dict_1
for key, value in dict_2.items():
dict_1[key] = value
print(dict_1)
Output:
{'Hello': 56,
'at': 23,
'test': 43,
'this': 20,
'where': 4,
'who': 5,
'why': 6}
Once again, if there are common keys in both dictionaries, they will be overwritten.
Conclusion
Python dictionary is a common built-in data type, which includes various key-value pairs. You can use various methods to add dictionary to dictionary in Python.
Leave a comment