Given Dictionary with value lists, extract all unique values.
Input : test_dict = {“Gfg” : [6, 7, 4, 6], “Geeks” : [6, 8, 5, 2]}
Output : [6, 7, 4, 8, 5, 2]
Explanation : All distincts elements extracted.Input : test_dict = {“Gfg” : [6, 7, 6], “Geeks” : [6, 8, 5, 2]}
Output : [6, 7, 8, 5, 2]
Explanation : All distincts elements extracted.
Method #1 : Using loop
This is brute way in which this task can be performed. In this, we memoize elements when they occur and prevent them from re enter to result list.
# Python3 code to demonstrate working of # Unique Dictionary Value List elements # Using loop # initializing dictionary test_dict = { "Gfg" : [ 6 , 7 , 4 , 6 ],
"is" : [ 8 , 9 , 5 ],
"for" : [ 2 , 5 , 3 , 7 ],
"Geeks" : [ 6 , 8 , 5 , 2 ]}
# printing original dictionary print ( "The original dictionary is : " + str (test_dict))
# list to memorize elements and insert result res = []
for sub in test_dict:
for ele in test_dict[sub]:
# testing for existence
if ele not in res:
res.append(ele)
# printing result print ( "Extracted items : " + str (res))
|
Output:
The original dictionary is : {‘Gfg’: [6, 7, 4, 6], ‘is’: [8, 9, 5], ‘for’: [2, 5, 3, 7], ‘Geeks’: [6, 8, 5, 2]}
Extracted items : [6, 7, 4, 8, 9, 5, 2, 3]
The time complexity of the given code is O(N*M), where N is the number of keys in the dictionary and M is the maximum length of the lists inside the dictionary.
The auxiliary space complexity of the code is O(N*M), where N is the number of keys in the dictionary and M is the maximum length of the lists inside the dictionary.
Method #2 : Using set() + union()
The combination of the above functions can be used to solve this problem. In this, we convert all lists using set() and then perform a union of all elements across all keys to get the desired result.
# Python3 code to demonstrate working of # Unique Dictionary Value List elements # Using set() + union() # initializing dictionary test_dict = { "Gfg" : [ 6 , 7 , 4 , 6 ],
"is" : [ 8 , 9 , 5 ],
"for" : [ 2 , 5 , 3 , 7 ],
"Geeks" : [ 6 , 8 , 5 , 2 ]}
# printing original dictionary print ( "The original dictionary is : " + str (test_dict))
# using * operator to get union res = list ( set ().union( * test_dict.values()))
# printing result print ( "Extracted items : " + str (res))
|
Output:
The original dictionary is : {‘Gfg’: [6, 7, 4, 6], ‘is’: [8, 9, 5], ‘for’: [2, 5, 3, 7], ‘Geeks’: [6, 8, 5, 2]}
Extracted items : [6, 7, 4, 8, 9, 5, 2, 3]
Method #3 : Using keys(),extend(),list(),set() methods
# Python3 code to demonstrate working of # Unique Dictionary Value List elements # Using loop # initializing dictionary test_dict = { "Gfg" : [ 6 , 7 , 4 , 6 ],
"is" : [ 8 , 9 , 5 ],
"for" : [ 2 , 5 , 3 , 7 ],
"Geeks" : [ 6 , 8 , 5 , 2 ]}
# printing original dictionary print ( "The original dictionary is : " + str (test_dict))
res = []
for i in test_dict.keys():
res.extend(test_dict[i])
res = list ( set (res))
# printing result print ( "Extracted items : " + str (res))
|
The original dictionary is : {'Gfg': [6, 7, 4, 6], 'is': [8, 9, 5], 'for': [2, 5, 3, 7], 'Geeks': [6, 8, 5, 2]} Extracted items : [2, 3, 4, 5, 6, 7, 8, 9]
Time complexity: This method involves iterating over all the values of the dictionary, so the time complexity is O(n*m), where n is the number of keys in the dictionary and m is the maximum length of the value list.
Auxiliary space: This method requires O(nm) auxiliary space as we are storing all the values of the dictionary in a list and then creating a set out of it. However, the space required will be less than O(nm) as duplicate values will be removed while creating the set.
Method #4 : Using items(),extend(),list(),Counter() methods
# Python3 code to demonstrate working of # Unique Dictionary Value List elements # Using Counter() function from collections import Counter
# initializing dictionary test_dict = { "Gfg" : [ 6 , 7 , 4 , 6 ],
"is" : [ 8 , 9 , 5 ],
"for" : [ 2 , 5 , 3 , 7 ],
"Geeks" : [ 6 , 8 , 5 , 2 ]}
# printing original dictionary print ( "The original dictionary is : " + str (test_dict))
res = []
for key, value in test_dict.items():
res.extend(value)
freq = Counter(res)
res = list (freq.keys())
res.sort() # printing result print ( "Extracted items : " + str (res))
|
The original dictionary is : {'Gfg': [6, 7, 4, 6], 'is': [8, 9, 5], 'for': [2, 5, 3, 7], 'Geeks': [6, 8, 5, 2]} Extracted items : [2, 3, 4, 5, 6, 7, 8, 9]
Time Complexity: O(n*m) Space Complexity: O(n*m)
Method #5 : reduce() function from the functools module:
Algorithm:
- Import the reduce() function from the functools module.
- Define the dictionary test_dict.
- Print the original dictionary test_dict.
- Use reduce() with a lambda function that performs set union (|) on each element in the dictionary’s values.
- This will create a set of all the unique values in the dictionary.
- Convert the set to a list.
- Print the list of unique values.
from functools import reduce
# 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 ( reduce ( lambda x, y: set (x) | set (y), test_dict.values()))
# printing result print ( "The unique values list is : " + str (result))
#This code is contributed by Rayudu . |
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:
The reduce() function iterates over all the values in the dictionary, and performs a set union operation on them. The time complexity of set union operation is O(n) where n is the number of elements in the sets. Therefore, the time complexity of this code is O(n*m), where n is the number of keys in the dictionary and m is the average number of elements in each list.
Auxiliary Space:
The space complexity is dominated by the space used to store the set of unique values. Since we are creating a set of all the values in the dictionary, the space complexity of this code is O(n), where n is the total number of elements in the dictionary.