Python Program to Concatenate Two Dictionaries

This is a Python Program to concatenate two dictionaries into one dictionary.

Problem Description

The program takes two dictionaries and concatenates them into one dictionary.

Problem Solution

1. Declare and initialize two dictionaries with some key-value pairs
2. Use the update() function to add the key-value pair from the second dictionary to the first dictionary.
3. Print the final dictionary.
4. Exit.

Program/Source Code

Here is source code of the Python Program to add a key-value pair to a dictionary. The program output is also shown below.

d1={'A':1,'B':2}
d2={'C':3}
d1.update(d2)
print("Concatenated dictionary is:")
print(d1)
Program Explanation

1. User must enter declare and initialize two dictionaries with a few key-value pairs and store it in separate variables.
2. The update() function is used to add the key-value pair from the second to the first dictionary.
3. The final updated dictionary is printed.

advertisement
advertisement
Runtime Test Cases
 
Case 1:
Concatenated dictionary is:
{'A': 1, 'C': 3, 'B': 2}

Sanfoundry Global Education & Learning Series – Python Programs.

To practice all Python programs, here is complete set of 150+ Python Problems and Solutions.

Note: Join free Sanfoundry classes at Telegram or Youtube

If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.