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 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)) |
The original list : [{‘gfg’: 1, ‘is’: 2}, {‘best’: 1, ‘for’: 3}, {‘CS’: 2}]
The unique values in list are : [1, 2, 3]
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 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)) |
The original list : [{‘gfg’: 1, ‘is’: 2}, {‘best’: 1, ‘for’: 3}, {‘CS’: 2}]
The unique values in list are : [1, 2, 3]
Recommended Posts:
- Python | Test if dictionary contains unique keys and values
- Python | Unique dictionary filter in list
- Python | Get unique values from a list
- Python | Summation of dictionary list values
- Python | Grouping list values into dictionary
- Python | Create a dictionary using list with none values
- Python | Combining values from dictionary of list
- Python | Element Occurrence in dictionary of list values
- Python | Initializing dictionary with list index values
- Python | Initializing dictionary with list index-values
- Python | Categorize tuple values into dictionary value list
- Python | Filter dictionary key based on the values in selective list
- Python dictionary | values()
- Python | Unique values in Matrix
- Python | Sum values for each key in nested dictionary
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



