Write Python Code to Merge Two Dictionaries Into a Single One

Hi Guys, welcome to my blog in this article you will learn about how to Write Python Code To Merge Two Dictionaries Into A Single One

 

Title : 

Write Python Code To Merge Two Dictionaries Into A Single One



Write Python Code to Merge Two Dictionaries Into a Single One


 

Description : 

Take two dictionaries and initialize the key value pair into these two dictionary variables. Now if we need to merge the two dictionaries into a single one then we can use the built-in function update to merge the two dictionaries into a single dictionary.


    
storage1 = {"apple": 23, "banana": 56, "orange": 89}
storage2 = {"strawberry": 90, "kiwi": 800, "lemon": 500}

print("The First Dictionary is ...")
print(storage1)

print("The Second Dictionary is ...")
print(storage1)

storage1.update(storage2)

    


After we must the two dictionary into into a single one then use print function to print the resulting dictionary


    
print("The Final Dictionary After Merging is ...")
print(storage1)

    




Complete Python Code : 


    
storage1 = {"apple": 23, "banana": 56, "orange": 89}
storage2 = {"strawberry": 90, "kiwi": 800, "lemon": 500}

print("The First Dictionary is ...")
print(storage1)

print("The Second Dictionary is ...")
print(storage1)

storage1.update(storage2)

print("The Final Dictionary After Merging is ...")
print(storage1)

    

 

 

Output : 


    
The First Dictionary is ...
{'apple': 23, 'banana': 56, 'orange': 89}

The Second Dictionary is ...
{'apple': 23, 'banana': 56, 'orange': 89}

The Final Dictionary After Merging is ...
{'apple': 23, 'banana': 56, 'orange': 89, 'strawberry': 90, 'kiwi': 800, 'lemon': 500}


    



Conclusion :

The above Python program uses the building function towards the to given dictionary into a single one Execute the above Python program by modifying the dictionary values according to your needs and must the to given dictionary using the above method Comment it down below if you have any queries regarding the above Python program, also comment if you have any suggestions to improve the about Python program

 

Post a Comment

0 Comments