Monday, November 18, 2024
Google search engine
HomeLanguagesPython | Get Unique values from list of dictionary

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




# Python3 code to demonstrate working of
# Get Unique values from list of dictionary
# Using set() + values() + dictionary comprehension
 
# Initialize list
test_list = [{'gfg': 1, 'is': 2}, {'best': 1, 'for': 3}, {'CS': 2}]
 
# printing original list
print("The original list : " + str(test_list))
 
# Using set() + values() + dictionary comprehension
# Get Unique values from list of dictionary
res = list(set(val for dic in test_list for val in dic.values()))
 
# printing result
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




# Python3 code to demonstrate working of
# Get Unique values from list of dictionary
# Using set() + values() + from_iterable()
from itertools import chain
 
# Initialize list
test_list = [{'gfg': 1, 'is': 2}, {'best': 1, 'for': 3}, {'CS': 2}]
 
# printing original list
print("The original list : " + str(test_list))
 
# Using set() + values() + from_iterable()
# Get Unique values from list of dictionary
res = list(set(chain.from_iterable(sub.values() for sub in test_list)))
 
# printing result
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




# Python3 code to demonstrate working of
# Get Unique values from list of dictionary
 
# Initialize list
test_list = [{'gfg' : 1, 'is' : 2}, {'best' : 1, 'for' : 3}, {'CS' : 2}]
 
# printing original list
print("The original list : " + str(test_list))
 
res=[]
for i in test_list:
    res.extend(list(i.values()))
res=list(set(res))
# printing result
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




# Python3 code to demonstrate working of
# Get Unique values from list of dictionary
# Using Counter() + set()
  
# Importing Counter from collections
from collections import Counter
  
# Initialize list
test_list = [{'gfg': 1, 'is': 2}, {'best': 1, 'for': 3}, {'CS': 2}]
  
# printing original list
print("The original list : " + str(test_list))
  
# Using Counter() + set()
# Get Unique values from list of dictionary
res = list(set(val for sub in test_list for val in Counter(sub).values()))
  
# printing result
print("The unique values in list are : " + str(res))
#This code is contributed by Edula Vinay Kumar Reddy


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)

RELATED ARTICLES

Most Popular

Recent Comments