- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Python Program to Concatenate Two Dictionaries Into One
When it is required to concatenate two dictionaries into a single entity, the ‘update’ method can be used.
A dictionary is a ‘key-value’ pair.
Below is a demonstration for the same −
Example
my_dict_1 = {'J':12,'W':22} my_dict_2 = {'M':67} print("The first dictionary is :") print(my_dict_1) print("The second dictionary is :") print(my_dict_2) my_dict_1.update(my_dict_2) print("The concatenated dictionary is :") print(my_dict_1)
Output
The first dictionary is : {'J': 12, 'W': 22} The second dictionary is : {'M': 67} The concatenated dictionary is : {'J': 12, 'W': 22, 'M': 67}
Explanation
Two dictionaries are defined, and are displayed on the console.
- The ‘update’ method is called on the first dictionary by passing second dictionary as parameter.
- This would help concatenate the dictionary.
- This is displayed on the console.
Advertisements