Key with Maximum Unique Values - Python
Last Updated :
10 Feb, 2025
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.
Python
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)
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.
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.
Python
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)
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.
Python
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)
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.
Python
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)
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.
Similar Reads
Keys with Maximum value - Python In Python, dictionaries are used to store data in key-value pairs and our task is to find the key or keys that have the highest value in a dictionary. For example, in a dictionary that stores the scores of students, you might want to know which student has the highest score. Different methods to ide
4 min read
Python | Tuples with maximum key of similar values Sometimes, while working with Python, we can have a problem in which we need to get all the records. This data can have similar values and we need to find maximum key-value pair. This kind of problem can occur while working with data. Let's discuss certain ways in which this task can be done. Method
6 min read
Python - Extract Item with Maximum Tuple Value Sometimes, while working with Python dictionaries, we can have a problem in which we need to extract the item with maximum value of value tuple index. This kind of problem can have application in domains such as web development. Let's discuss certain ways in which this task can be performed. Input :
5 min read
Python - Unique values count of each Key Given a Dictionaries list, the task is to write a Python program to count the unique values of each key. Example: Input : test_list = [{"gfg" : 1, "is" : 3, "best": 2}, {"gfg" : 1, "is" : 3, "best" : 6}, {"gfg" : 7, "is" : 3, "best" : 10}] Output : {'gfg': 2, 'is': 1, 'best': 3} Explanation : gfg ha
7 min read
Maximum String Value Length of Key - Python The task of finding the maximum string length of a specific key in a list of dictionaries involves identifying the longest string associated with the given key. Given a list of dictionaries, the goal is to extract the string values of the specified key, compute their lengths and determine the maximu
3 min read
Python - Unique Values of Key in Dictionary We are given a list of dictionaries and our task is to extract all the unique values associated with a given key. For example, consider: data = [ {"name": "Aryan", "age": 25}, {"name": "Harsh", "age": 30}, {"name": "Kunal", "age": 22}, {"name": "Aryan", "age": 27}]key = "name"Then, the unique values
4 min read
Python - Row with Maximum Record Element Sometimes, while working with Python Records, we can have a problem in which we need to find the row with maximum record element. This kind of problem can come in domains of web development and day-day programming. Let's discuss certain ways in which this task can be performed. Input : test_list = [
7 min read
Python - Maximum record value key in dictionary Sometimes, while working with dictionary records, we can have a problem in which we need to find the key with maximum value of a particular key of nested records in list. This can have applications in domains such as web development and Machine Learning. Lets discuss certain ways in which this task
6 min read
Python - List with most unique elements Sometimes, while working with data, we can have a problem in which we need to compute the list which has most number of unique elements. This can have application in many domains. Lets discuss certain ways in which this task can be performed. Method #1 : Using list comprehension + max() + set() The
8 min read
Python - Maximum of Similar Keys in Tuples Sometimes, while working with Python tuples, we can have a problem in which we need to perform maximum of all values of the equal keys in Tuple list. This kind of application in useful in many domains such as web development and day-day programming. Let's discuss certain ways in which this task can
4 min read