Open In App

Python – Minimum value key assignment

Last Updated : 23 Apr, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Given two dictionaries, the task is to write a Python program to assign minimum values for matching keys from both dictionaries.

Examples:

Input : test_dict1 = {“gfg” : 1, “is” : 7, “best” : 8}, test_dict2 = {“gfg” : 2, “is” : 2, “best” : 10}
Output : {“gfg” : 1, “is” : 2, “best” : 8}
Explanation : Minimum of 1 and 2 is 1, hence, gfg is assigned with 1 and so on.

Input : test_dict1 = {“gfg” : 1, “is” : 7, “best” : 12}, test_dict2 = {“gfg” : 2, “is” : 2, “best” : 10}
Output : {“gfg” : 1, “is” : 2, “best” : 10}
Explanation : Minimum of 10 and 12 is 10, hence, best is assigned with 10 and so on.

Method #1 : Using dictionary comprehension + min() + items()

In this, minimum of keys’ values are extracted using min(). Dictionary comprehension is used to reconstruct the dictionary with modified values.

Python3




# Python3 code to demonstrate working of
# Minimum value key assignment
# Using dictionary comprehension + min() + items()
 
# initializing dictionaries
test_dict1 = {"gfg": 1, "is": 7, "best": 8}
test_dict2 = {"gfg": 2, "is": 2, "best": 10}
 
# printing original dictionaries
print("The original dictionary 1 is : " + str(test_dict1))
print("The original dictionary 2 is : " + str(test_dict2))
 
# using min() to assign min values
res = {key: min(val, test_dict2[key]) for key, val in test_dict1.items()}
 
# printing result
print("The minimum value keys : " + str(res))


Output

The original dictionary 1 is : {'gfg': 1, 'is': 7, 'best': 8}
The original dictionary 2 is : {'gfg': 2, 'is': 2, 'best': 10}
The minimum value keys : {'gfg': 1, 'is': 2, 'best': 8}

Method #2 : Using dict() + min() + zip()

In this, for better comparison, zip() is used for getting values to compare, min() is used to get minimum values of keys. Then dict() is used to conversion of result to dictionary.

Python3




# Python3 code to demonstrate working of
# Minimum value key assignment
# Using dict() + min() + zip()
 
# initializing dictionaries
test_dict1 = {"gfg": 1, "is": 7, "best": 8}
test_dict2 = {"gfg": 2, "is": 2, "best": 10}
 
# printing original dictionaries
print("The original dictionary 1 is : " + str(test_dict1))
print("The original dictionary 2 is : " + str(test_dict2))
 
# using min() to assign min values
# dict() for conversion of result to dictionary
res = dict([min(i, j) for i, j in zip(test_dict1.items(), test_dict2.items())])
 
# printing result
print("The minimum value keys : " + str(res))


Output

The original dictionary 1 is : {'gfg': 1, 'is': 7, 'best': 8}
The original dictionary 2 is : {'gfg': 2, 'is': 2, 'best': 10}
The minimum value keys : {'gfg': 1, 'is': 2, 'best': 8}

Method #3: Using union

Approach

using union to find the minimum keys

Algorithm

1. Create an empty dictionary called result_dict.
2. For each key in the union of test_dict1 and test_dict2, check if it exists in both dictionaries.
3. If it exists in both dictionaries, assign the minimum value between the two dictionaries to the key in result_dict.
4. If it exists only in test_dict1, assign the value of the key in test_dict1 to result_dict.
5. If it exists only in test_dict2, assign the value of the key in test_dict2 to result_dict.
6. Return result_dict.

Python3




def minimum_value_key_assignment(test_dict1, test_dict2):
    # Create an empty dictionary called `result_dict`.
    result_dict = {}
    # For each key in the union of `test_dict1` and `test_dict2`, check if it exists in both dictionaries.
    for key in set(test_dict1.keys()) | set(test_dict2.keys()):
        # If it exists in both dictionaries, assign the minimum value between the two dictionaries to the key in `result_dict`.
        if key in test_dict1 and key in test_dict2:
            result_dict[key] = min(test_dict1[key], test_dict2[key])
        # If it exists only in `test_dict1`, assign the value of the key in `test_dict1` to `result_dict`.
        elif key in test_dict1:
            result_dict[key] = test_dict1[key]
        # If it exists only in `test_dict2`, assign the value of the key in `test_dict2` to `result_dict`.
        else:
            result_dict[key] = test_dict2[key]
    # Return `result_dict`.
    return result_dict
test_dict1 = {'gfg' : 1, 'is' : 7, 'best' : 8}
test_dict2 = {'gfg' : 2, 'is' : 2, 'best' : 10}
print( minimum_value_key_assignment(test_dict1, test_dict2))


Output

{'gfg': 1, 'best': 8, 'is': 2}

Time complexity: O(n), where n is the number of keys in both dictionaries.
Auxiliary Space: O(n), where n is the number of keys in both dictionaries.

Method 4 : using the built-in function setdefault().

 step-by-step approach:

Initialize an empty dictionary result_dict.
Loop through the keys of test_dict1 and test_dict2 using the set() function to get a set of all unique keys in both dictionaries.
For each key, use the min() function to get the minimum value between the two dictionaries using the get() method to avoid raising a KeyError if a key is missing in one of the dictionaries.
Use the setdefault() method to update result_dict with the minimum value key assignment for each key.
Return result_dict.

Python3




# Python3 code to demonstrate working of
# Minimum value key assignment
# Using setdefault()
 
# initializing dictionaries
test_dict1 = {"gfg": 1, "is": 7, "best": 8}
test_dict2 = {"gfg": 2, "is": 2, "best": 10}
 
# printing original dictionaries
print("The original dictionary 1 is : " + str(test_dict1))
print("The original dictionary 2 is : " + str(test_dict2))
 
# using setdefault() to assign min values
result_dict = {}
for key in set(test_dict1.keys()) | set(test_dict2.keys()):
    result_dict.setdefault(key, min(test_dict1.get(key, float('inf')), test_dict2.get(key, float('inf'))))
 
# printing result
print("The minimum value keys : " + str(result_dict))


Output

The original dictionary 1 is : {'gfg': 1, 'is': 7, 'best': 8}
The original dictionary 2 is : {'gfg': 2, 'is': 2, 'best': 10}
The minimum value keys : {'is': 2, 'best': 8, 'gfg': 1}

The time complexity of this approach is O(n), where n is the total number of unique keys in both dictionaries. The auxiliary space complexity is also O(n), as we create a new dictionary with all the unique keys and their minimum value assignments.



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads