Open In App
Related Articles

Python – Cross mapping of Two dictionary value lists

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Report issue
Report

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

# Python3 code to demonstrate working of
# Cross mapping of Two dictionary value lists
# Using loop + setdefault() + extend()
 
# initializing dictionaries
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]}
 
# printing original lists
print("The original dictionary 1 is : " + str(test_dict1))
print("The original dictionary 2 is : " + str(test_dict2))
 
res = {}
 
# getting all values of first dictionary
for key, val in test_dict1.items():
    for key1 in val:
         
        # getting result with default value list and extending
        # according to value obtained from get()
        res.setdefault(key, []).extend(test_dict2.get(key1, []))
 
# printing result
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

# Python3 code to demonstrate working of
# Cross mapping of Two dictionary value lists
# Using list comprehension + dictionary comprehension
 
# initializing dictionaries
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]}
 
# printing original lists
print("The original dictionary 1 is : " + str(test_dict1))
print("The original dictionary 2 is : " + str(test_dict2))
 
# using internal and external comprehension to
# solve problem
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()}
 
# printing result
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

# Initializing dictionaries
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]}
 
# Printing original dictionaries
print("The original dictionary 1 is :", test_dict1)
print("The original dictionary 2 is :", test_dict2)
 
# Performing cross mapping of two dictionary value lists
dict3 = {key: list(set(val1).intersection(test_dict2.keys())) for key, val1 in test_dict1.items()}
 
# Replacing keys with values from dict2
for key, val in dict3.items():
    dict3[key] = [item for k in val for item in test_dict2[k]]
 
# Printing the constructed dictionary
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:

  1. Initialize the two dictionaries.
  2. Use a dictionary comprehension to iterate through the keys and values of the first dictionary.
  3. 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.
  4. Assign the combined list to the key in a new dictionary.
  5. Print the resulting dictionary.

Python3

#initializing dictionaries
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]}
 
#printing original lists
print("The original dictionary 1 is : " + str(test_dict1))
print("The original dictionary 2 is : " + str(test_dict2))
 
#using map() and lambda to extend values to new dict
res = {k: sum(map(lambda i: test_dict2.get(i, []), v), []) for k, v in test_dict1.items()}
 
#printing result
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.



Last Updated : 17 Apr, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads