Last Updated : 12 Jul, 2025
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.
Extract Unique values dictionary values 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
# 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], 'is': [10, 11, 7, 5], 'best': [6, 12, 10, 8], 'for': [1, 2, 5]} The unique values list is : [1, 2, 5, 6, 7, 8, 10, 11, 12]
Time Complexity: O(nlogn)
Auxiliary Space: O(n)
This performs the task in similar way. The difference is that the task of set comprehension is performed using chain().
Python3
# 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], 'is': [10, 11, 7, 5], 'best': [6, 12, 10, 8], 'for': [1, 2, 5]} The unique values list is : [1, 2, 5, 6, 7, 8, 10, 11, 12]
The time complexity of the code is O(nlog(n)) where n is the total number of elements in all the lists of the dictionary.
The auxiliary space complexity of the code is O(n) because it creates a new list of all the values in the dictionary using the values() method, which requires O(n) space.
Extract Unique values dictionary values Using extend() and sort() methods Python3
# Python3 code to demonstrate working of
# Extract Unique values dictionary values
# 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))
x=list(test_dict.values())
y=[]
res=[]
for i in x:
y.extend(i)
for i in y:
if i not in res:
res.append(i)
res.sort()
# printing result
print("The unique values list is : " + str(res))
The original dictionary is : {'gfg': [5, 6, 7, 8], 'is': [10, 11, 7, 5], 'best': [6, 12, 10, 8], 'for': [1, 2, 5]} The unique values list is : [1, 2, 5, 6, 7, 8, 10, 11, 12]
Time Complexity: O(nlogn)
Auxiliary Space: O(n)
# Python3 code to demonstrate working of
# Extract Unique values dictionary values
# initializing dictionary
from collections import Counter
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))
valuesList = []
for key, values in test_dict.items():
for value in values:
valuesList.append(value)
freq = Counter(valuesList)
uniqueValues = list(freq.keys())
uniqueValues.sort()
# printing result
print("The unique values list is : " + str(uniqueValues))
The original dictionary is : {'gfg': [5, 6, 7, 8], 'is': [10, 11, 7, 5], 'best': [6, 12, 10, 8], 'for': [1, 2, 5]} The unique values list is : [1, 2, 5, 6, 7, 8, 10, 11, 12]
The time complexity of the above program is O(nmlog(m)), where n is the number of keys in the dictionary and m is the average number of values per key.
Auxiliary space complexity is O(n*m).
Extract Unique values dictionary values Using Operator.countOf() methodSTEPS:
# Python3 code to demonstrate working of
# Extract Unique values dictionary values
import operator as op
# 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))
x=list(test_dict.values())
y=[]
res=[]
for i in x:
y.extend(i)
for i in y:
if op.countOf(res,i)==0:
res.append(i)
res.sort()
# printing result
print("The unique values list is : " + str(res))
The original dictionary is : {'gfg': [5, 6, 7, 8], 'is': [10, 11, 7, 5], 'best': [6, 12, 10, 8], 'for': [1, 2, 5]} The unique values list is : [1, 2, 5, 6, 7, 8, 10, 11, 12]
Auxiliary Space: O(N*N)
Time Complexity:O(N)
Extract Unique values dictionary values Using set() + sum() Python3
#Python3 code to demonstrate working of
#Extract Unique values dictionary values
#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
result = list(set(sum(test_dict.values(), [])))
#printing result
print("The unique values list is : " + str(result))
The original dictionary is : {'gfg': [5, 6, 7, 8], 'is': [10, 11, 7, 5], 'best': [6, 12, 10, 8], 'for': [1, 2, 5]} The unique values list is : [1, 2, 5, 6, 7, 8, 10, 11, 12]
Time complexity: O(n)
Auxiliary Space: O(n)
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4