Given two dictionaries. The task is to merge them in such a way that the resulting dictionary contains the key from the first dictionary and value from the second dictionary.
Examples:
Input : test_dict1 = {“Gfg” : 20, “is” : 36, “best” : 100}, test_dict2 = {“Gfg2” : 26, “is2” : 20, “best2” : 70}
Output : {‘Gfg’: 26, ‘is’: 20, ‘best’: 70}
Explanation : Similar index keys’ values assigned to dictionary 1.Input : test_dict1 = {“Gfg” : 20, “best” : 100}, test_dict2 = {“Gfg2” : 26, “best2” : 70}
Output : {‘Gfg’: 26, ‘best’: 70}
Explanation : Similar index keys’ values assigned to dictionary 1.
Method #1 : Using loop + keys()
This is one way in which this task can be performed. In this, we extract all the keys using keys() and then assign required values inside loop.
Python3
# Python3 code to demonstrate working of # Assign similar index values in Dictionary # Using loop + keys() # initializing dictionaries test_dict1 = {"Gfg" : 20, "is" : 36, "best" : 100} test_dict2 = {"Gfg2" : 26, "is2" : 19, "best2" : 70} # printing original dictionaries print("The original dictionary 1 is : " + str(test_dict1)) print("The original dictionary 2 is : " + str(test_dict2)) # extracting keys and values keys1 = list(test_dict1.keys()) vals2 = list(test_dict2.values()) # assigning new values res = dict() for idx in range(len(keys1)): res[keys1[idx]] = vals2[idx] # printing result print("Mapped dictionary : " + str(res)) |
The original dictionary 1 is : {‘Gfg’: 20, ‘is’: 36, ‘best’: 100}
The original dictionary 2 is : {‘Gfg2’: 26, ‘is2’: 19, ‘best2’: 70}
Mapped dictionary : {‘Gfg’: 26, ‘is’: 19, ‘best’: 70}
Method #2 : Using zip() + values()
This is yet another way in which this task can be performed. In this, we perform the task of mapping using zip(), extracting values using values().
Python3
# Python3 code to demonstrate working of # Assign similar index values in Dictionary # Using zip() + values() # initializing dictionaries test_dict1 = {"Gfg" : 20, "is" : 36, "best" : 100} test_dict2 = {"Gfg2" : 26, "is2" : 19, "best2" : 70} # printing original dictionaries print("The original dictionary 1 is : " + str(test_dict1)) print("The original dictionary 2 is : " + str(test_dict2)) # using zip() to perform required dict. mapping res = dict(zip(test_dict1, test_dict2.values())) # printing result print("Mapped dictionary : " + str(res)) |
The original dictionary 1 is : {‘Gfg’: 20, ‘is’: 36, ‘best’: 100}
The original dictionary 2 is : {‘Gfg2’: 26, ‘is2’: 19, ‘best2’: 70}
Mapped dictionary : {‘Gfg’: 26, ‘is’: 19, ‘best’: 70}
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.
Recommended Posts:
- Python | Combine the values of two dictionaries having same key
- Python - Convert Dictionaries List to Order Key Nested dictionaries
- Combine keys in a list of dictionaries in Python
- Python | Sort Python Dictionaries by Key or Value
- Python - Extract Key's Value, if Key Present in List and Dictionary
- Python | Segregating key's value in list of dictionaries
- Python - Sort dictionaries list by Key's Value list index
- Python - Extract Key's value from Mixed Dictionaries List
- Python - Extract dictionaries with Empty String value in K key
- Python | Combine two dictionary adding values for common keys
- Python | Combine two lists by maintaining duplicates in first list
- Python - Convert list of dictionaries to Dictionary Value list
- Python - Remove dictionary from a list of dictionaries if a particular value is not present
- Python - Create a Dictionary with Key as First Character and Value as Words Starting with that Character
- Python | Sum list of dictionaries with same key
- Python | Get values of particular key in list of dictionaries
- Python - Add custom values key in List of dictionaries
- Python - Remove Duplicate Dictionaries characterized by Key
- Python - Remove Dictionaries with Matching Values with K Key
- Python - Combine dictionary with priority
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

