GIven two dictionaries with list values, perform mapping of keys of first list with values of other list, by checking values-key linkage.
Input : test_dict1 = {“Gfg” : [4, 10], “Best” : [8, 6], “is” : [9, 3]}, test_dict2 = {6 : [15, 9], 8 : [6, 3], 7 : [9, 8], 9 : [10, 11]} Output : {‘Best’: [6, 3, 15, 9], ‘is’: [10, 11]} Explanation : “Best” has 8 and 6, which are mapped to 6, 3 and 15, 9 hence output for that key. Input : test_dict1 = {“Gfg” : [4, 10], “Best” : [18, 16], “is” : [9, 3]}, test_dict2 = {6 : [15, 9], 8 : [6, 3], 7 : [9, 8], 9 : [10, 11]} Output : {‘is’: [10, 11]} Explanation : Only 9 present as possible key.
Method #1 : Using loop + setdefault() + extend()
The combination of above functions can be used to solve this problem. In this, we perform the task of getting the matching keys with values using get() and setdefault is used to construct empty list for mapping.
Python3
test_dict1 = { "Gfg" : [ 4 , 7 ], "Best" : [ 8 , 6 ], "is" : [ 9 , 3 ]}
test_dict2 = { 6 : [ 15 , 9 ], 8 : [ 6 , 3 ], 7 : [ 9 , 8 ], 9 : [ 10 , 11 ]}
print ( "The original dictionary 1 is : " + str (test_dict1))
print ( "The original dictionary 2 is : " + str (test_dict2))
res = {}
for key, val in test_dict1.items():
for key1 in val:
res.setdefault(key, []).extend(test_dict2.get(key1, []))
print ( "The constructed dictionary : " + str (res))
|
Output
The original dictionary 1 is : {'Gfg': [4, 7], 'Best': [8, 6], 'is': [9, 3]}
The original dictionary 2 is : {6: [15, 9], 8: [6, 3], 7: [9, 8], 9: [10, 11]}
The constructed dictionary : {'Gfg': [9, 8], 'Best': [6, 3, 15, 9], 'is': [10, 11]}
Time Complexity: O(n*n) where n is the number of elements in the dictionary.
Auxiliary Space: O(n), where n is the number of elements in the dictionary.
Method #2 : Using list comprehension + dictionary comprehension
This is one more way in which this problem can be solved. In this, we extract all the mapping using list comprehension and then construct new dictionary by cross-mapping the extracted values.
Python3
test_dict1 = { "Gfg" : [ 4 , 7 ], "Best" : [ 8 , 6 ], "is" : [ 9 , 3 ]}
test_dict2 = { 6 : [ 15 , 9 ], 8 : [ 6 , 3 ], 7 : [ 9 , 8 ], 9 : [ 10 , 11 ]}
print ( "The original dictionary 1 is : " + str (test_dict1))
print ( "The original dictionary 2 is : " + str (test_dict2))
res = {key: [j for i in val if i in test_dict2 for j in test_dict2[i]]
for key, val in test_dict1.items()}
print ( "The constructed dictionary : " + str (res))
|
Output
The original dictionary 1 is : {'Gfg': [4, 7], 'Best': [8, 6], 'is': [9, 3]}
The original dictionary 2 is : {6: [15, 9], 8: [6, 3], 7: [9, 8], 9: [10, 11]}
The constructed dictionary : {'Gfg': [9, 8], 'Best': [6, 3, 15, 9], 'is': [10, 11]}
Time complexity: O(nmk), where n is the number of key-value pairs in test_dict1, m is the average length of the value lists in test_dict1, and k is the average length of the value lists in test_dict2.
Auxiliary space: O(n*m), where n is the number of key-value pairs in test_dict1 and m is the maximum length of the value lists in test_dict1.
Method #3: Using dictionary comprehensions, loops, set operations
- Initializing two dictionaries
- Using a dictionary comprehension to iterate over each key-value pair in test_dict1.
- For each key-value pair, iterate over each key-value pair in test_dict2. If the key in dict2 is in the value list of the current key in dict1, calculate the intersection of the two value lists and store the result
- Printing the dictionary.
Python3
test_dict1 = { 'Gfg' : [ 4 , 7 ], 'Best' : [ 8 , 6 ], 'is' : [ 9 , 3 ]}
test_dict2 = { 6 : [ 15 , 9 ], 8 : [ 6 , 3 ], 7 : [ 9 , 8 ], 9 : [ 10 , 11 ]}
print ( "The original dictionary 1 is :" , test_dict1)
print ( "The original dictionary 2 is :" , test_dict2)
dict3 = {key: list ( set (val1).intersection(test_dict2.keys())) for key, val1 in test_dict1.items()}
for key, val in dict3.items():
dict3[key] = [item for k in val for item in test_dict2[k]]
print ( "The constructed dictionary :" , dict3)
|
Output
The original dictionary 1 is : {'Gfg': [4, 7], 'Best': [8, 6], 'is': [9, 3]}
The original dictionary 2 is : {6: [15, 9], 8: [6, 3], 7: [9, 8], 9: [10, 11]}
The constructed dictionary : {'Gfg': [9, 8], 'Best': [6, 3, 15, 9], 'is': [10, 11]}
Time Complexity: O(N*M) where n and m are the sizes of the two dictionaries.
Auxiliary Space: O(N) as we are creating a new dictionary.
Method #4: Using map() and lambda function
Step by step Algorithm:
- Initialize the two dictionaries.
- Use a dictionary comprehension to iterate through the keys and values of the first dictionary.
- For each value in the current key-value pair, use a lambda function to get the value from the second dictionary that matches the current value, and then use sum() and [] to combine the values into a list.
- Assign the combined list to the key in a new dictionary.
- Print the resulting dictionary.
Python3
test_dict1 = { "Gfg" : [ 4 , 7 ], "Best" : [ 8 , 6 ], "is" : [ 9 , 3 ]}
test_dict2 = { 6 : [ 15 , 9 ], 8 : [ 6 , 3 ], 7 : [ 9 , 8 ], 9 : [ 10 , 11 ]}
print ( "The original dictionary 1 is : " + str (test_dict1))
print ( "The original dictionary 2 is : " + str (test_dict2))
res = {k: sum ( map ( lambda i: test_dict2.get(i, []), v), []) for k, v in test_dict1.items()}
print ( "The constructed dictionary : " + str (res))
|
Output
The original dictionary 1 is : {'Gfg': [4, 7], 'Best': [8, 6], 'is': [9, 3]}
The original dictionary 2 is : {6: [15, 9], 8: [6, 3], 7: [9, 8], 9: [10, 11]}
The constructed dictionary : {'Gfg': [9, 8], 'Best': [6, 3, 15, 9], 'is': [10, 11]}
Time Complexity: O(N*M) where n and m are the sizes of the two dictionaries.
Auxiliary Space: O(N) as we are creating a new dictionary.
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Last Updated :
17 Apr, 2023
Like Article
Save Article