Sometimes, while working with data, we can have problem in which we need to perform the extraction of only unique values from dictionary values list. This can have application in many domains such as web development. Lets discuss certain ways in which this task can be performed.
Method #1 : Using sorted() + set comprehension + values()
The combination of above functionalities can be used to perform this task. In this, we extract all the values using values() and set comprehension is used to get unique values compiled in list.
# Python3 code to demonstrate working of # Extract Unique values dictionary values # Using set comprehension + values() + sorted() # initializing dictionary test_dict = {'gfg' : [5, 6, 7, 8], 'is' : [10, 11, 7, 5], 'best' : [6, 12, 10, 8], 'for' : [1, 2, 5]} # printing original dictionary print("The original dictionary is : " + str(test_dict)) # Extract Unique values dictionary values # Using set comprehension + values() + sorted() res = list(sorted({ele for val in test_dict.values() for ele in val})) # printing result print("The unique values list is : " + str(res)) |
The original dictionary is : {‘gfg’: [5, 6, 7, 8], ‘best’: [6, 12, 10, 8], ‘is’: [10, 11, 7, 5], ‘for’: [1, 2, 5]}
The unique values list is : [1, 2, 5, 6, 7, 8, 10, 11, 12]
Method #2 : Using chain() + sorted() + values()
This performs the task in similar way. The difference is that the task of set comprehension is performed using chain().
# Python3 code to demonstrate working of # Extract Unique values dictionary values # Using chain() + sorted() + values() from itertools import chain # initializing dictionary test_dict = {'gfg' : [5, 6, 7, 8], 'is' : [10, 11, 7, 5], 'best' : [6, 12, 10, 8], 'for' : [1, 2, 5]} # printing original dictionary print("The original dictionary is : " + str(test_dict)) # Extract Unique values dictionary values # Using chain() + sorted() + values() res = list(sorted(set(chain(*test_dict.values())))) # printing result print("The unique values list is : " + str(res)) |
The original dictionary is : {‘gfg’: [5, 6, 7, 8], ‘best’: [6, 12, 10, 8], ‘is’: [10, 11, 7, 5], ‘for’: [1, 2, 5]}
The unique values list is : [1, 2, 5, 6, 7, 8, 10, 11, 12]
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.
Recommended Posts:
- Python program to extract the Unique Dictionary Value List elements
- Ways to extract all dictionary values | Python
- Python | Extract filtered Dictionary Values
- Python - Extract Dictionary values list to List
- Python - Extract Numerical Dictionary values
- Python program to extract dictionary items for custom values
- Python | Test if dictionary contains unique keys and values
- Python | Get Unique values from list of dictionary
- Python - Unique value keys in a dictionary with lists as values
- Python - Unique Values of Key in Dictionary
- Python - Extract ith column values from jth column values
- Python - Extract values of Particular Key in Nested Values
- Python - Filter dictionary values in heterogenous dictionary
- Python - Append Dictionary Keys and Values ( In order ) in dictionary
- Python program to update a dictionary with the values from a dictionary list
- Python | Extract unique tuples from list, Order Irrespective
- Python - Extract Unique value key pairs
- Python | Extract key-value of dictionary in variables
- Python | Extract specific keys from dictionary
- Python - Extract Equal Pair 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.

