Python – Unique Values of Key in Dictionary
Last Updated :
05 May, 2023
Sometimes, while working with Python dictionaries, we can have a problem in which we need to extract the unique values of a particular key in Dictionary List. This kind of problem in very common in day-day programming and web development domain. Let us discuss certain ways in which this task can be performed.
Input : test_list = [{‘geeks’: 10, ‘for’: 8, ‘best’: 10}, {‘best’: 10}]
Output : [10]
Input : test_list = [{‘best’: 11}]
Output : [11]
Method #1 : Using loop + set()
The combination of above functions can be used to solve this problem. In this, we extract all the elements of key in loop and then convert the extracted list to set to get unique values.
Python3
test_list = [{ 'Gfg' : 5 , 'is' : 6 , 'best' : 7 , 'for' : 8 , 'geeks' : 10 },
{ 'Gfg' : 9 , 'is' : 4 , 'best' : 7 , 'for' : 4 , 'geeks' : 7 },
{ 'Gfg' : 2 , 'is' : 10 , 'best' : 8 , 'for' : 9 , 'geeks' : 3 }]
print ( "The original list is : " + str (test_list))
op_key = 'best'
res = []
for sub in test_list:
res.append(sub[op_key])
res = list ( set (res))
print ( "The unique values of key : " + str (res))
|
Output :
The original list is : [{‘for’: 8, ‘best’: 7, ‘is’: 6, ‘Gfg’: 5, ‘geeks’: 10}, {‘for’: 4, ‘best’: 7, ‘is’: 4, ‘Gfg’: 9, ‘geeks’: 7}, {‘for’: 9, ‘best’: 8, ‘is’: 10, ‘Gfg’: 2, ‘geeks’: 3}] The unique values of key : [8, 7]
Time Complexity: O(n) where n is the number of elements in the list “test_list”.
Auxiliary Space: O(n) where n is the number of elements in the list “test_list”.
Method #2: Using list comprehension This is yet another way to solve this problem. In this, we perform in a similar way as above method but in a shorthand approach.
Python3
test_list = [{ 'Gfg' : 5 , 'is' : 6 , 'best' : 7 , 'for' : 8 , 'geeks' : 10 },
{ 'Gfg' : 9 , 'is' : 4 , 'best' : 7 , 'for' : 4 , 'geeks' : 7 },
{ 'Gfg' : 2 , 'is' : 10 , 'best' : 8 , 'for' : 9 , 'geeks' : 3 }]
print ( "The original list is : " + str (test_list))
op_key = 'best'
res = list ( set (sub[op_key] for sub in test_list))
print ( "The unique values of key : " + str (res))
|
Output :
The original list is : [{‘for’: 8, ‘best’: 7, ‘is’: 6, ‘Gfg’: 5, ‘geeks’: 10}, {‘for’: 4, ‘best’: 7, ‘is’: 4, ‘Gfg’: 9, ‘geeks’: 7}, {‘for’: 9, ‘best’: 8, ‘is’: 10, ‘Gfg’: 2, ‘geeks’: 3}] The unique values of key : [8, 7]
Time Complexity: O(n) where n is the number of elements in the dictionary. The list comprehension is used to perform the task and it takes O(n) time.
Auxiliary Space: O(n) additional space of size n is created where n is the number of elements in the dictionary.
Method #3:Using Counter() function
Python3
from collections import Counter
test_list = [{ 'Gfg' : 5 , 'is' : 6 , 'best' : 7 , 'for' : 8 , 'geeks' : 10 },
{ 'Gfg' : 9 , 'is' : 4 , 'best' : 7 , 'for' : 4 , 'geeks' : 7 },
{ 'Gfg' : 2 , 'is' : 10 , 'best' : 8 , 'for' : 9 , 'geeks' : 3 }]
print ( "The original list is : " + str (test_list))
op_key = 'best'
res = []
for sub in test_list:
res.append(sub[op_key])
freq = Counter(res)
res = list (freq.keys())
print ( "The unique values of key : " + str (res))
|
Output
The original list is : [{'Gfg': 5, 'is': 6, 'best': 7, 'for': 8, 'geeks': 10}, {'Gfg': 9, 'is': 4, 'best': 7, 'for': 4, 'geeks': 7}, {'Gfg': 2, 'is': 10, 'best': 8, 'for': 9, 'geeks': 3}]
The unique values of key : [7, 8]
Auxiliary Space: O(N)
Time Complexity:O(N)
Method 4: Using the map() function and the set() function.
Approach:
- Import the Counter module from the collections library to count the frequency of elements in the list.
- Initialize the list of dictionaries containing the data to be processed.
- Define the key whose unique values we want to find.
- Create a lambda function to extract the value of the desired key from each dictionary.
- Apply the lambda function to each dictionary in the list using the map() function.
- Store the results in a list.
- Count the frequency of each element in the list using the Counter() function.
- Extract the keys from the counter object using the keys() method and store them in a list.
- Print the list of unique values.
Python3
from collections import Counter
test_list = [{ 'Gfg' : 5 , 'is' : 6 , 'best' : 7 , 'for' : 8 , 'geeks' : 10 },
{ 'Gfg' : 9 , 'is' : 4 , 'best' : 7 , 'for' : 4 , 'geeks' : 7 },
{ 'Gfg' : 2 , 'is' : 10 , 'best' : 8 , 'for' : 9 , 'geeks' : 3 }]
print ( "The original list is : " + str (test_list))
op_key = 'best'
extract_value = lambda dictionary: dictionary[op_key]
results = list ( map (extract_value, test_list))
freq = Counter(results)
unique_values = list (freq.keys())
print ( "The unique values of key : " + str (unique_values))
|
Output
The original list is : [{'Gfg': 5, 'is': 6, 'best': 7, 'for': 8, 'geeks': 10}, {'Gfg': 9, 'is': 4, 'best': 7, 'for': 4, 'geeks': 7}, {'Gfg': 2, 'is': 10, 'best': 8, 'for': 9, 'geeks': 3}]
The unique values of key : [7, 8]
Time complexity: The time complexity of this approach is O(n) because it only involves iterating over the list once to extract the values of the desired key and O(k) for counting the frequency of the unique values, where k is the number of unique values of the desired key.
Auxiliary space: The auxiliary space used by this approach is O(n+k), where n is the length of the list and k is the number of unique values of the desired key.
Method #6: Using itertools.chain.from_iterable() and set()
Step-by-step approach:
Import the itertools module
Use the chain.from_iterable() function from itertools to flatten the list of dictionary values corresponding to the given key
Convert the flattened iterable to set to get the unique values for the given key.
Python3
import itertools
test_list = [{ 'Gfg' : 5 , 'is' : 6 , 'best' : 7 , 'for' : 8 , 'geeks' : 10 },
{ 'Gfg' : 9 , 'is' : 4 , 'best' : 7 , 'for' : 4 , 'geeks' : 7 },
{ 'Gfg' : 2 , 'is' : 10 , 'best' : 8 , 'for' : 9 , 'geeks' : 3 }]
print ( "The original list is : " + str (test_list))
op_key = 'best'
res_set = set (itertools.chain.from_iterable([sub[op_key]] for sub in test_list))
res = list (res_set)
print ( "The unique values of key : " + str (res))
|
Output
The original list is : [{'Gfg': 5, 'is': 6, 'best': 7, 'for': 8, 'geeks': 10}, {'Gfg': 9, 'is': 4, 'best': 7, 'for': 4, 'geeks': 7}, {'Gfg': 2, 'is': 10, 'best': 8, 'for': 9, 'geeks': 3}]
The unique values of key : [8, 7]
Time complexity: O(n), where n is the total number of values in the list of dictionaries.
Auxiliary space: O(k), where k is the number of unique values corresponding to the given key.
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...