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. This is useful for retrieving the most significant or relevant information from the data.

Problem Statement

Given a dictionary with tuples as values, we need to find the key corresponding to the tuple with the maximum value (typically the second element of the tuple).

Example

Input:

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

Expected 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.

Method 1: Using max() with List Comprehension

This approach first finds the maximum tuple using max() with a lambda function, then uses list comprehension to find the corresponding key ?

# Input dictionary having values as tuples 
inputDict = {'hello': ("p", 2), 'tutorialspoint': ("c", 15), 'python': ("k", 8)}

print("Input dictionary:", inputDict)

# Find the maximum tuple based on the second element
maximumValue = max(inputDict.values(), key=lambda k: k[1])

# Get the key with maximum value using list comprehension
maxKey = [k for k, v in inputDict.items() if v == maximumValue][0]

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

How It Works

  • max(inputDict.values(), key=lambda k: k[1]) finds the tuple with the highest second element

  • List comprehension [k for k, v in inputDict.items() if v == maximumValue] finds all keys matching the maximum tuple

  • [0] gets the first (and typically only) matching key

Method 2: Using max() with next() Function

This method uses next() to find the first key that matches the maximum tuple, which is more memory-efficient than list comprehension ?

# Input dictionary having values as tuples 
inputDict = {'hello': ("p", 2), 'tutorialspoint': ("c", 15), 'python': ("k", 8)}

print("Input dictionary:", inputDict)

# Find maximum tuple based on the second element
maximumValue = max(inputDict.values(), key=lambda k: k[1])

# Get the key with maximum value using next() function
maxKey = next(k for k, v in inputDict.items() if v == maximumValue)

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

How It Works

  • next() returns the first item from the generator expression

  • More memory-efficient as it doesn't create a full list

  • Stops iteration as soon as the first match is found

Method 3: Direct Approach Using max() on Dictionary Items

The most efficient approach is to directly apply max() on dictionary items ?

# Input dictionary having values as tuples 
inputDict = {'hello': ("p", 2), 'tutorialspoint': ("c", 15), 'python': ("k", 8)}

print("Input dictionary:", inputDict)

# Directly find the key with maximum tuple value
maxKey = max(inputDict, key=lambda k: inputDict[k][1])

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

Comparison

Method Memory Usage Performance Best For
List Comprehension Higher Good When you need all matching keys
next() Function Lower Better When you need only the first match
Direct max() Lowest Best Most efficient single-pass solution

Conclusion

Use max(dict, key=lambda k: dict[k][1]) for the most efficient solution. For more complex filtering, use next() with generator expressions to save memory compared to list comprehension.

Updated on: 2026-03-27T12:55:11+05:30

317 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements