Key with Maximum Unique Values - Python
The task involves finding the key whose list contains the most unique values in a dictionary. Each list is converted to a set to remove duplicates and the key with the longest set is identified. For example, in d = {"A": [1, 2, 2], "B": [3, 4, 5, 3], "C": [6, 7, 7, 8]}, key "C" has the most unique values ({6, 7, 8} â 3 elements).
Using max()
max() allows us to find the largest item in an iterable. It provides a key parameter, which lets us specify a custom function for comparison. By using len(set(d[k])), we count the number of unique values in each list and determine the key with the highest count.
d = {"Gfg": [5, 7, 5, 4, 5], "is": [6, 7, 4, 3, 3], "Best": [9, 9, 6, 5, 5]}
max_key = max(d, key=lambda k: len(set(d[k])))
print(max_key)
Output
is
Explanation: key argument in max() uses lambda k: len(set(d[k])), which converts each keyâs list to a set and measures its length. The key with the most unique values is stored in max_key.
Table of Content
Using sorted()
sorted() sorts dictionary keys based on a given condition. By sorting the keys based on the number of unique values and selecting the last element, we get the key with the maximum unique values.
d = {"Gfg": [5, 7, 5, 4, 5], "is": [6, 7, 4, 3, 3], "Best": [9, 9, 6, 5, 5]}
max_key = sorted(d, key=lambda k: len(set(d[k])))[-1]
print(max_key)
Output
is
Explanation: key argument in sorted() uses lambda k: len(set(d[k])), which converts each keyâs list to a set and measures its length. The keys are sorted by unique value count and [-1] selects the key with the highest count, storing it in max_key.
Using counter
Counter class from Python's collections module counts the occurrences of elements in an iterable. While typically used for frequency analysis, it can also be used to count unique elements in a list.
from collections import Counter
d = {"Gfg": [5, 7, 5, 4, 5], "is": [6, 7, 4, 3, 3], "Best": [9, 9, 6, 5, 5]}
max_key = max(d, key=lambda k: len(Counter(d[k])))
print(max_key)
Output
is
Explanation: key argument in max() uses lambda k: len(Counter(d[k])), which creates a Counter object for each keyâs list to count element occurrences. The length of the Counter gives the number of unique values. The key with the highest unique count is stored in max_key.
Using for loop
For loop allows iterating through the dictionary while keeping track of the maximum unique count. This approach is explicit and easy to understand but requires additional code.
d = {"Gfg": [5, 7, 5, 4, 5], "is": [6, 7, 4, 3, 3], "Best": [9, 9, 6, 5, 5]}
max_key, max_val = None, 0
for k, v in d.items():
unique_count = len(set(v))
if unique_count > max_val:
max_val, max_key = unique_count, k
print(max_key)
Output
is
Explanation: This code iterates through the dictionary using for k, v in d.items(), where v is converted to a set to count unique values. If the current unique count is greater than max_val, max_val and max_key are updated. The key with the most unique values is stored in max_key.