Open In App

Python – Assign values to Values List

Improve
Improve
Like Article
Like
Save
Share
Report

Given 2 dictionaries, assign values to value list elements mapping from dictionary 2.

Input : test_dict = {‘Gfg’ : [3, 6], ‘best’ :[9]}, look_dict = {3 : [1, 5], 6 : “Best”, 9 : 12} Output : {‘Gfg’: {3: [1, 5], 6: ‘Best’}, ‘best’: {9: 12}} Explanation : 3 is replaced by key 3 and value [1, 5] and so on. Input : test_dict = {‘Gfg’ : [3, 6]}, look_dict = {3 : [1, 5], 6 : “Best”} Output : {‘Gfg’: {3: [1, 5], 6: ‘Best’}} Explanation : 3 is replaced by key 3 and value [1, 5] and so on.

Method #1 : Using nested dictionary comprehension

In this, we use inner dictionary comprehension to map values elements to dict 2, and outer dict is used to extract all keys from dictionary 1.

Python3




# Python3 code to demonstrate working of
# Assign values to Values List
# Using nested dictionary comprehension
 
# initializing dictionary
test_dict = {'Gfg' : [3, 6],
             'is' : [4, 2],
             'best' :[9]}
 
# printing original dictionary
print("The original dictionary is : " + str(test_dict))
 
# initializing lookup dict
look_dict = {3 : [1, 5], 6 : "Best", 4 : 10, 9 : 12, 2 : "CS"}
 
# nested dictionaries to sought solution
res = {idx: {ikey: look_dict[ikey] for ikey in test_dict[idx]} for idx in test_dict}
 
# printing result
print("The mapped dictionary : " + str(res))


Output

The original dictionary is : {'Gfg': [3, 6], 'is': [4, 2], 'best': [9]}
The mapped dictionary : {'Gfg': {3: [1, 5], 6: 'Best'}, 'is': {4: 10, 2: 'CS'}, 'best': {9: 12}}

Time Complexity: O(N*M), where N is the number of key-value pairs in the test_dict and M is number of values in any value list.
Auxiliary space: O(N + K), where N is the number of key-value pairs in the test_dict and K is the number of keys in the look_dict.

Method #2 : Using items() + dictionary comprehension

Similar to above method, another one-liner, difference being that items() is used for element access.

Python3




# Python3 code to demonstrate working of
# Assign values to Values List
# Using items() + dictionary comprehension
 
# initializing dictionary
test_dict = {'Gfg': [3, 6],
             'is': [4, 2],
             'best': [9]}
 
# printing original dictionary
print("The original dictionary is : " + str(test_dict))
 
# initializing lookup dict
look_dict = {3: [1, 5], 6: "Best", 4: 10, 9: 12, 2: "CS"}
 
# nested dictionaries to sought solution
# items() used to access key-val pairs
res = {key: {ikey: ival for (ikey, ival) in look_dict.items(
) if ikey in val} for (key, val) in test_dict.items()}
 
# printing result
print("The mapped dictionary : " + str(res))


Output

The original dictionary is : {'Gfg': [3, 6], 'is': [4, 2], 'best': [9]}
The mapped dictionary : {'Gfg': {3: [1, 5], 6: 'Best'}, 'is': {4: 10, 2: 'CS'}, 'best': {9: 12}}

Method#3: Using for loop

Approach

this approach uses dictionary comprehension to create a new dictionary with values assigned based on the look_dict.

Algorithm

1. Create a set of all the values in the look_dict.
2. Iterate over each key-value pair in the test_dict.
3. Create a new dictionary with the same keys as the test_dict and an empty dictionary as the value.
Iterate over each value in the value list of the current key.
4. If the value is present in the set of values from the look_dict, then add a key-value pair to the value dictionary with the key as the value and the value as the corresponding value from the look_dict.
5. Return the new dictionary.

Python3




def assign_values(test_dict, look_dict):
    values_set = set(look_dict.keys())
    new_dict = {key: {} for key in test_dict}
    for key, values in test_dict.items():
        for value in values:
            if value in values_set:
                new_dict[key][value] = look_dict[value]
    return new_dict
 
test_dict = {'Gfg' : [3, 6],
             'is' : [4, 2],
             'best' :[9]}
look_dict = {3 : [1, 5], 6 : "Best", 4 : 10, 9 : 12, 2 : "CS"}
print(assign_values(test_dict, look_dict))


Output

{'Gfg': {3: [1, 5], 6: 'Best'}, 'is': {4: 10, 2: 'CS'}, 'best': {9: 12}}

Time Complexity: O(NM), where N is the number of key-value pairs in the test_dict and M is the maximum number of values in any value list.
Space Complexity: O(N + K), where N is the number of key-value pairs in the test_dict and K is the number of keys in the look_dict.

Method #4: Using defaultdict and for loop

  1. Import the defaultdict module from the collections library.
  2. Create a dictionary called test_dict that contains the original dictionary data.
  3. Create a dictionary called look_dict that contains the lookup data.
  4. Create an empty defaultdict called res to store the result.
  5. Iterate through each key-value pair in test_dict using the items() method.
  6. For each key-value pair, iterate through the values using a for loop.
  7. For each value, use it as a key to look up the corresponding value in look_dict.
  8. Add a new key-value pair to the corresponding dictionary in res, where the key is the current value and the value is the value from look_dict that was looked up in the previous step.
  9. Repeat steps 6-8 for all values in the current key-value pair.
  10. Repeat steps 5-9 for all key-value pairs in test_dict.
  11. Print the resulting res

Python3




from collections import defaultdict
 
# initializing dictionary
test_dict = {'Gfg' : [3, 6],
             'is' : [4, 2],
             'best' :[9]}
 
# initializing lookup dict
look_dict = {3 : [1, 5], 6 : "Best", 4 : 10, 9 : 12, 2 : "CS"}
 
# creating a defaultdict to store the result
res = defaultdict(dict)
 
# iterating through the test_dict and its values
for key, values in test_dict.items():
    # iterating through the values
    for value in values:
        # using the value to lookup the corresponding values in the look_dict
        res[key][value] = look_dict[value]
 
# printing result
print("The mapped dictionary : " + str(res))


Output

The mapped dictionary : defaultdict(<class 'dict'>, {'Gfg': {3: [1, 5], 6: 'Best'}, 'is': {4: 10, 2: 'CS'}, 'best': {9: 12}})

Time complexity: O(NM), where N is the number of keys in the test_dict and M is the average number of values per key. 
Auxiliary space: O(NM), since we are storing the result in a dictionary.



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