![]() |
VOOZH | about |
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.
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.
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
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:
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.