Python Program to find the key of Maximum Value Tuples in a Dictionary


Finding the key of the maximum value tuples in a python dictionary means identifying the key associated with the tuple that has the highest value among all the tuples in the dictionary, which can be useful for retrieving the most significant or relevant information from the data.

Example

Assume we have taken an input dictionary having values as tuples. We will now find the key of maximum value tuples in an input dictionary using the above methods.

Input

inputDict = {'hello': ("p", 2), 'tutorialspoint': ("c", 15), 'python': ("k", 8)}

Output

The key of the maximum value in a tuple:  tutorialspoint

In the above input dictionary, the maximum value in a tuple is 15 and its corresponding key is tutorialspoint.

Methods Used

The following are the various methods to accomplish this task:

  • Using list comprehension, max() and values() functions

  • Using max(), values() and next() functions

Method 1: Using list comprehension, max() and values() functions

In this method we are going to understand how to use comprehension max() and min() function to find the key of maximum value tuples in a dictionary.

List Comprehension

When you wish to build a new list based on the values of an existing list, list comprehension provides a shorter/concise syntax.

new_list = [expression for item in iterable if condition]

max() function

The max() method(returns the highest-valued item/greatest numbers in an iterable).

max_value = max(iterable)

dict.values() function

The dict.values() method provides a view object that displays a list of all the values in the dictionary in order of insertion.

value_list = dictionary.values()

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

Algorithm (Steps)

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

  • Create a variable to store the input dictionary containing values as a tuple.

  • Print the input dictionary.

  • Pass the given dictionary and key as a lambda function to find the maximum values of the dictionary.

  • Get the key with the above maximum value by iterating through the key-value pairs of the dictionary using the items() function and list comprehension.

  • Print the resultant dictionary key of the maximum value in a tuple.

Example

The following program returns a dictionary key of the maximum value in a tuple using list comprehension, max(), and values() functions

# input dictionary having values as tuples 
inputDict = {'hello': ("p", 2), 'tutorialspoint': ("c", 15), 'python': ("k", 8)}
# printing input dictionary
print("Input dictionary:", inputDict)
# getting the maximum value from the values of the input dictionary 
# i.e, tuple 
maximumValue = max(inputDict.values(), key=lambda k: k[1])
# getting key with maximum value using list comprehension
maxKey = [k for k, v in inputDict.items() if v == maximumValue][0]
# printing the resultant dictionary key of the maximum value in a tuple
print("The key of the maximum value in a tuple: ", maxKey)

Output

On executing, the above program will generate the following output

Input dictionary: {'hello': ('p', 2), 'tutorialspoint': ('c', 15), 'python': ('k', 8)}
The key of the maximum value in a tuple:  tutorialspoint

Method 2: Using max(), values() and next() functions

This method is going to demonstrate how to use max(), value() and next() function in python to find the key of maximum value tuple in dictionary.

lambda function

A lambda function is an anonymous function that is small.

A lambda function can have an unlimited/any number of arguments but only one expression.

Syntax

lambda arguments : expression

next() function

returns the next item in an iterator like a list, string tuple, etc. If the iterable reaches its end, you can add a default return value to return.

Syntax

next(iterable, default)

Algorithm (Steps)

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

  • Create a variable to store the input dictionary containing values as a tuple.

  • Print the input dictionary

  • Get the maximum value of the dictionary by passing the given dictionary and key as a lambda function.

  • Traverse in the given dictionary items using the next() function and find the key with maximum value using the above max variable.

  • Print the resultant dictionary key of the maximum value in a tuple.

Example

The following program returns a dictionary key of the maximum value in a tuple using max(), values(), and next() functions

# input dictionary having values as tuples 
inputDict = {'hello': ("p", 2), 'tutorialspoint': ("c", 15), 'python': ("k", 8)}
# printing input dictionary
print("Input dictionary:", inputDict)
# Getting maximum value from the dictionary using max() function
maximumValue = max(inputDict.values(), key=lambda k: k[1])
# Getting the maximum value key using the next() function
maxKey = next(k for k, v in inputDict.items() if v == maximumValue)
# printing the resultant dictionary key of the maximum value in a tuple
print("The key of the maximum value in a tuple: ", maxKey)

Output

On executing, the above program will generate the following output

Input dictionary: {'hello': ('p', 2), 'tutorialspoint': ('c', 15), 'python': ('k', 8)}
The key of the maximum value in a tuple:  tutorialspoint

Conclusion

In this article, we have learned 2 different methods to find the key of maximum value tuples in a dictionary. Using the dictionary, we learned how to traverse between the dictionary's elements using items(.) Additionally, we learned how to use the next() function to add an iterator to the dictionary. Last but not least, we learned how to use any custom function as a key in the max() function.

Updated on: 17-Aug-2023

85 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements