Python - Unique value keys in a dictionary with lists as values
Last Updated :
27 Apr, 2023
Sometimes, while working with Python dictionaries, we can have problem in which we need to extract keys with values that are unique (there should be at least one item not present in other lists), i.e doesn't occur in any other key's value lists. This can have applications in data preprocessing. Lets discuss certain ways in which this task can be performed.
Method #1 : Using loop + count() The combination of above functionalities can be used to solve this problem. In this, we perform the task of counting occurrence using count and extraction and testing is done using loop using conditional statement.
Python3
# Python3 code to demonstrate working of
# Unique Keys Values
# Using loop + count()
# initializing dictionary
test_dict = {'Gfg' : [6, 5], 'is' : [6, 10, 5], 'best' : [12, 6, 5]}
# printing original dictionary
print("The original dictionary is : " + str(test_dict))
# Unique Keys Values
# Using loop + count()
temp = [sub for ele in test_dict.values() for sub in ele]
res = []
for key, vals in test_dict.items():
for val in vals:
if temp.count(val) == 1:
res.append(key)
break
# printing result
print("The unique values keys are : " + str(res))
Output : The original dictionary is : {'Gfg': [6, 5], 'best': [12, 6, 5], 'is': [6, 10, 5]}
The unique values keys are : ['best', 'is']
Time Complexity: O(n*n)
Auxiliary Space: O(n)
Method #2 : Using list comprehension + any() + count() The combination of above functions can be used to perform this task. In this, we check for the unique elements using any() and count(). This is one liner way in which this task can be performed.
Python3
# Python3 code to demonstrate working of
# Unique Keys Values
# Using list comprehension + any() + count()
# initializing dictionary
test_dict = {'Gfg' : [6, 5], 'is' : [6, 10, 5], 'best' : [12, 6, 5]}
# printing original dictionary
print("The original dictionary is : " + str(test_dict))
# Unique Keys Values
# Using list comprehension + any() + count()
res = [key for key, vals in test_dict.items() if any([ele for sub in test_dict.values()
for ele in set(sub)].count(idx) == 1 for idx in vals)]
# printing result
print("The unique values keys are : " + str(res))
Output : The original dictionary is : {'Gfg': [6, 5], 'best': [12, 6, 5], 'is': [6, 10, 5]}
The unique values keys are : ['best', 'is']
Time Complexity: O(n), where n is the length of the list test_list
Auxiliary Space: O(n) additional space of size n is created where n is the number of elements in the res list
Method #3 : Using Counter function
Python3
# Python3 code to demonstrate working of
# Unique Keys Values
from collections import Counter
# initializing dictionary
test_dict = {'Gfg': [6, 5], 'is': [6, 10, 5], 'best': [12, 6, 5]}
# printing original dictionary
print("The original dictionary is : " + str(test_dict))
values = []
for key, value in test_dict.items():
for i in value:
values.append(i)
freq_values = Counter(values)
res = []
for key, value in test_dict.items():
for i in value:
if freq_values[i] == 1:
res.append(key)
break
# printing result
print("The unique values keys are : " + str(res))
OutputThe original dictionary is : {'Gfg': [6, 5], 'is': [6, 10, 5], 'best': [12, 6, 5]}
The unique values keys are : ['is', 'best']
Method #4: Using operator.countOf() method:
Python3
# Python3 code to demonstrate working of
# Unique Keys Values
import operator as op
# initializing dictionary
test_dict = {'Gfg': [6, 5], 'is': [6, 10, 5], 'best': [12, 6, 5]}
# printing original dictionary
print("The original dictionary is : " + str(test_dict))
# Unique Keys Values
# Using loop + count()
temp = [sub for ele in test_dict.values() for sub in ele]
res = []
for key, vals in test_dict.items():
for val in vals:
if op.countOf(temp, val) == 1:
res.append(key)
break
# printing result
print("The unique values keys are : " + str(res))
OutputThe original dictionary is : {'Gfg': [6, 5], 'is': [6, 10, 5], 'best': [12, 6, 5]}
The unique values keys are : ['is', 'best']
Time Complexity: O(N)
Auxiliary Space: O(N)
Similar Reads
Python - Keys associated with value list in dictionary Sometimes, while working with Python dictionaries, we can have a problem finding the key of a particular value in the value list. This problem is quite common and can have applications in many domains. Let us discuss certain ways in which we can Get Keys associated with Values in the Dictionary in P
4 min read
Python | Test if dictionary contains unique keys and values Sometimes, we just wish to work with unique elements and any type of repetition is not desired, for these cases, we need to have techniques to solve these problems. One such problem can be to test for unique keys and values. For keys, they are by default unique, hence no external testing is required
6 min read
Dictionary with Tuple as Key in Python Dictionaries allow a wide range of key types, including tuples. Tuples, being immutable, are suitable for use as dictionary keys when storing compound data. For example, we may want to map coordinates (x, y) to a specific value or track unique combinations of values. Let's explores multiple ways to
4 min read
How to use a List as a key of a Dictionary in Python 3? In Python, we use dictionaries to check if an item is present or not . Dictionaries use key:value pair to search if a key is present or not and if the key is present what is its value . We can use integer, string, tuples as dictionary keys but cannot use list as a key of it . The reason is explained
3 min read
Get Unique Values from a List in Python Getting unique values from a list in Python allows you to remove duplicates and work with distinct elements. For example, given the list a = [1, 2, 1, 1, 3, 4, 3, 3, 5], you might want to extract the unique values [1, 2, 3, 4, 5]. There are various efficient methods to extract unique values from a l
3 min read
How to Add Same Key Value in Dictionary Python Dictionaries are powerful data structures that allow us to store key-value pairs. However, one common question that arises is how to handle the addition of values when the keys are the same. In this article, we will see different methods to add values for the same dictionary key using Python.Adding
2 min read