Python | Get Unique values from list of dictionary
Sometimes, while working with Python dictionaries, we can have a problem in which we need to find the unique values over all the dictionaries in a list. This kind of utility can occur in case while working with similar data and we wish to extract the unique ones. Let’s discuss certain ways in which this task can be performed.
Method #1 : Using set() + values() + dictionary comprehension The combination of these methods can together help us achieve the task of getting the unique values. The values function helps us get the values of dictionary, set helps us to get the unique of them, and dictionary comprehension to iterate through the list.
Python3
test_list = [{ 'gfg' : 1 , 'is' : 2 }, { 'best' : 1 , 'for' : 3 }, { 'CS' : 2 }]
print ( "The original list : " + str (test_list))
res = list ( set (val for dic in test_list for val in dic.values()))
print ( "The unique values in list are : " + str (res))
|
Output :
The original list : [{‘gfg’: 1, ‘is’: 2}, {‘best’: 1, ‘for’: 3}, {‘CS’: 2}] The unique values in list are : [1, 2, 3]
Time Complexity: O(n)
Auxiliary Space: O(n)
Method #2 : Using set() + values() + from_iterable() The combination of above functions can be used to perform this particular task. It is just as the above method, but the iteration part is done by the from_iterable function.
Python3
from itertools import chain
test_list = [{ 'gfg' : 1 , 'is' : 2 }, { 'best' : 1 , 'for' : 3 }, { 'CS' : 2 }]
print ( "The original list : " + str (test_list))
res = list ( set (chain.from_iterable(sub.values() for sub in test_list)))
print ( "The unique values in list are : " + str (res))
|
Output :
The original list : [{‘gfg’: 1, ‘is’: 2}, {‘best’: 1, ‘for’: 3}, {‘CS’: 2}] The unique values in list are : [1, 2, 3]
Time Complexity: O(n), where n is the length of the list test_dict
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 extend(),values(),list(),set() methods
Python3
test_list = [{ 'gfg' : 1 , 'is' : 2 }, { 'best' : 1 , 'for' : 3 }, { 'CS' : 2 }]
print ( "The original list : " + str (test_list))
res = []
for i in test_list:
res.extend( list (i.values()))
res = list ( set (res))
print ( "The unique values in list are : " + str (res))
|
Output
The original list : [{'gfg': 1, 'is': 2}, {'best': 1, 'for': 3}, {'CS': 2}]
The unique values in list are : [1, 2, 3]
Method #4 : Using Counter() + set()
Python3
from collections import Counter
test_list = [{ 'gfg' : 1 , 'is' : 2 }, { 'best' : 1 , 'for' : 3 }, { 'CS' : 2 }]
print ( "The original list : " + str (test_list))
res = list ( set (val for sub in test_list for val in Counter(sub).values()))
print ( "The unique values in list are : " + str (res))
|
Output
The original list : [{'gfg': 1, 'is': 2}, {'best': 1, 'for': 3}, {'CS': 2}]
The unique values in list are : [1, 2, 3]
Time Complexity: O(n)
Auxiliary Space: O(n)
Last Updated :
27 Apr, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...