Python Program to Minimum Value Key Assignment


The meaning of value key assignment in Python is the process of associating a value with a specific key within a dictionary, allowing efficient retrieval and manipulation of data based on key-value pairs. It enables the creation and organization of structured data structures for various purposes

Python's implementation of a data structure known more commonly as an associative array is a dictionary. A dictionary is made up of a group of key-value pairs. Each key-value combination corresponds to a key and its corresponding value.

Example

Assume we have taken two input dictionaries with the same keys. We will now compare the values of each key in both dictionaries and assign the minimum value from it to the key and print a resultant dictionary using the above methods.

Input

inputDict_1 = {"hello": 2, "tutorialspoint": 10, "users": 3}
inputDict_2 = {"hello": 6, "tutorialspoint": 5, "users": 7}

Output

Resultant dictionary after assigning minimum value keys:
 {'hello': 2, 'tutorialspoint': 5, 'users': 3}

In above both dictionaries, 2 is less than 6, hence we assigned the hello key with a minimum value of 2.

In the same way for the key tutorialspoint, 10 is greater than 5, hence 5 is the minimum.

For users key, 3 is less than 7, therefore 3 is the minimum.

Methods Used

The following are the various methods to accomplish this task:

  • Using dictionary comprehension, min() and items() functions

  • Using dict(), min() and zip() functions

Method 1: Using dictionary comprehension, min() and items() functions

min() function (returns the smallest/minimum value in an iterable)

items() function(returns a view object i.e, it contains the key-value pairs of the dictionary, as tuples in a list).

minimum_value = min(iterable)
items_list = dictionary.items()

Algorithm (Steps)

Following are the Algorithm/steps to be followed to perform the desired task

  • Create a variable to store the input dictionary 1.

  • Create another variable to store the input dictionary 2.

  • Print the input dictionaries.

  • Traverse in the first dictionary using the items() function.

  • Check the minimum value of the first dictionary key and second dictionary value(As the key is common we get it directly using the [] operator).

  • Use the dictionary comprehension to store the common key with minimum value.

  • Print the resultant dictionary after assigning minimum value keys.

Example

The following program returns a dictionary after assigning minimum value keys using dictionary comprehension, min(), and items() functions

# input dictionary 1
inputDict_1 = {"hello": 2, "tutorialspoint": 10, "users": 3}
# input dictionary 2
inputDict_2 = {"hello": 6, "tutorialspoint": 5, "users": 7}
# printing input dictionaries
print("Input dictionary 1: ", inputDict_1)
print("Input dictionary 2: ", inputDict_2)
# Getting the minimum value of the keys from the first and second dictionaries.
# Assigning them with the common key with the dictionary comprehension
resultantDict = {k: min(v, inputDict_2[k]) for k, v in inputDict_1.items()}
# printing resultant dictionary
print("Resultant dictionary after assigning minimum value keys:\n", resultantDict)

Output

Input dictionary 1:  {'hello': 2, 'tutorialspoint': 10, 'users': 3}
Input dictionary 2:  {'hello': 6, 'tutorialspoint': 5, 'users': 7}
Resultant dictionary after assigning minimum value keys:
 {'hello': 2, 'tutorialspoint': 5, 'users': 3}

Method 2: Using dict(), min() and zip() functions

dict() function(converts into a dictionary)

The zip() function can be used to combine two lists/iterators.

zip(*iterables)

Algorithm (Steps)

Following are the Algorithm/steps to be followed to perform the desired task

  • Iterate in both dictionaries in a combined way using the zip function.

  • Find the minimum value of the keys using the min() function.

  • Print the resultant dictionary after assigning minimum value keys.

Example

The following program returns a dictionary after assigning minimum value keys using dict(), min() and zip() functions

# input dictionary 1
inputDict_1 = {"hello": 2, "tutorialspoint": 10, "users": 3}
# input dictionary 2
inputDict_2 = {"hello": 6, "tutorialspoint": 5, "users": 7}
# printing input dictionaries
print("Input dictionary 1: ", inputDict_1)
print("Input dictionary 2: ", inputDict_2)
# Iterate in both dictionaries by passing them in the zip() function.
# Get the minimum value of these both dictionaries using the min() function
resultantDict = dict([min(i, j) for i, j in zip(
    inputDict_1.items(), inputDict_2.items())])
# printing resultant dictionary
print("Resultant dictionary after assigning minimum value keys:\n", resultantDict)

Output

Input dictionary 1:  {'hello': 2, 'tutorialspoint': 10, 'users': 3}
Input dictionary 2:  {'hello': 6, 'tutorialspoint': 5, 'users': 7}
Resultant dictionary after assigning minimum value keys:
 {'hello': 2, 'tutorialspoint': 5, 'users': 3}

Conclusion

In this article, we have learned 2 different methods for Minimum value key assignment. Additionally, we learned how to use the zip function to combine iterations through two dictionaries or iterables. Finally, we learned how to apply some dictionary operations and iterate across the dictionary using dictionary comprehension.

Updated on: 17-Aug-2023

55 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements