Python – Extract Unique values dictionary values
Last Updated :
27 Jul, 2023
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
test_dict = { 'gfg' : [ 5 , 6 , 7 , 8 ],
'is' : [ 10 , 11 , 7 , 5 ],
'best' : [ 6 , 12 , 10 , 8 ],
'for' : [ 1 , 2 , 5 ]}
print ( "The original dictionary is : " + str (test_dict))
res = list ( sorted ({ele for val in test_dict.values() for ele in val}))
print ( "The unique values list is : " + str (res))
|
Output
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)
Extract Unique values dictionary values 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
from itertools import chain
test_dict = { 'gfg' : [ 5 , 6 , 7 , 8 ],
'is' : [ 10 , 11 , 7 , 5 ],
'best' : [ 6 , 12 , 10 , 8 ],
'for' : [ 1 , 2 , 5 ]}
print ( "The original dictionary is : " + str (test_dict))
res = list ( sorted ( set (chain( * test_dict.values()))))
print ( "The unique values list is : " + str (res))
|
Output
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
test_dict = { 'gfg' : [ 5 , 6 , 7 , 8 ],
'is' : [ 10 , 11 , 7 , 5 ],
'best' : [ 6 , 12 , 10 , 8 ],
'for' : [ 1 , 2 , 5 ]}
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()
print ( "The unique values list is : " + str (res))
|
Output
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)
Extract Unique values dictionary values Using Counter(),append() and sort() methods
- Import the Counter class from the collections module.
- Define a dictionary test_dict with string keys and lists of integers as values.
- Print the original dictionary.
- Create an empty list called valuesList.
- Iterate over the dictionary using the items() method to access the keys and values. For each key-value pair, iterate over the values list and append each value to valuesList.
- Use the Counter() function to count the frequency of each value in valuesList. This creates a dictionary where the keys are the unique values in valuesList and the values are their frequencies.
- Use the keys() method to extract a list of the unique values from freq.
- Sort the uniqueValues list in ascending order.
- Print the final list of unique values
Python3
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 ]}
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()
print ( "The unique values list is : " + str (uniqueValues))
|
Output
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() method
STEPS:
- Import the “operator” module as “op”.
- Create a dictionary called “test_dict” and initialize it with some key-value pairs where each key is a string and the value is a list of integers.
- Print the original dictionary using the “print()” function.
- Create an empty list called “x”.
- Get all the values of the dictionary and append them to the list “x” using the “list()” function and the “values()” method of the dictionary.
- Create an empty list called “y”.
- Iterate over each item in the list “x” using a for loop and append all the elements of each list to the list “y” using the “extend()” method.
- Create an empty list called “res”.
- Iterate over each item in the list “y” using a for loop.
- Check if the count of the current item in the list “res” is zero using the “countOf()” method of the “operator” module.
- If the count is zero, append the current item to the list “res” using the “append()” method.
- Sort the list “res” using the “sort()” method.
- Print the unique values list using the “print()” function.
Python3
import operator as op
test_dict = { 'gfg' : [ 5 , 6 , 7 , 8 ],
'is' : [ 10 , 11 , 7 , 5 ],
'best' : [ 6 , 12 , 10 , 8 ],
'for' : [ 1 , 2 , 5 ]}
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()
print ( "The unique values list is : " + str (res))
|
Output
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
test_dict = { 'gfg' : [ 5 , 6 , 7 , 8 ],
'is' : [ 10 , 11 , 7 , 5 ],
'best' : [ 6 , 12 , 10 , 8 ],
'for' : [ 1 , 2 , 5 ]}
print ( "The original dictionary is : " + str (test_dict))
result = list ( set ( sum (test_dict.values(), [])))
print ( "The unique values list is : " + str (result))
|
Output
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)
Please Login to comment...