Python program to extract the Unique Dictionary Value List elements
Last Updated :
08 May, 2023
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
test_dict = { "Gfg" : [ 6 , 7 , 4 , 6 ],
"is" : [ 8 , 9 , 5 ],
"for" : [ 2 , 5 , 3 , 7 ],
"Geeks" : [ 6 , 8 , 5 , 2 ]}
print ( "The original dictionary is : " + str (test_dict))
res = []
for sub in test_dict:
for ele in test_dict[sub]:
if ele not in res:
res.append(ele)
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
test_dict = { "Gfg" : [ 6 , 7 , 4 , 6 ],
"is" : [ 8 , 9 , 5 ],
"for" : [ 2 , 5 , 3 , 7 ],
"Geeks" : [ 6 , 8 , 5 , 2 ]}
print ( "The original dictionary is : " + str (test_dict))
res = list ( set ().union( * test_dict.values()))
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
test_dict = { "Gfg" : [ 6 , 7 , 4 , 6 ],
"is" : [ 8 , 9 , 5 ],
"for" : [ 2 , 5 , 3 , 7 ],
"Geeks" : [ 6 , 8 , 5 , 2 ]}
print ( "The original dictionary is : " + str (test_dict))
res = []
for i in test_dict.keys():
res.extend(test_dict[i])
res = list ( set (res))
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 : [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
from collections import Counter
test_dict = { "Gfg" : [ 6 , 7 , 4 , 6 ],
"is" : [ 8 , 9 , 5 ],
"for" : [ 2 , 5 , 3 , 7 ],
"Geeks" : [ 6 , 8 , 5 , 2 ]}
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()
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 : [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.
Python3
from functools import reduce
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 ( reduce ( lambda x, y: set (x) | set (y), 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:
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.
Similar Reads
Python | Test if element is dictionary value
Sometimes, while working with a Python dictionary, we have a specific use case in which we just need to find if a particular value is present in the dictionary as it's any key's value. This can have use cases in any field of programming one can think of. Let's discuss certain ways in which this prob
4 min read
Python - Extract dictionary items with List elements
Sometimes, while working with Python dictionaries, we can have a problem in which we need to extract all the items from dictionary that constitute all the elements from a list. This kind of problem can occur in domains such as competitive programming and web development. Let's discuss certain ways i
8 min read
Python - Extract Kth index elements from Dictionary Value list
Given a dictionary with list as values, extract all the Kth index elements. Input : {"Gfg" : [4, 7, 5], "Best" : [8, 6, 7], "is" : [9, 3, 8]}, K = 2 Output : [5, 7, 8] Explanation : The 2nd index elements are 5, 7 and 8 respectively in different keys. Input : {"Gfg" : [4, 7, 5], "Best" : [8, 6, 7],
7 min read
Python program to unique keys count for Value in Tuple List
Given dual tuples, get a count of unique keys for each value present in the tuple. Input : test_list = [(3, 4), (1, 2), (2, 4), (8, 2), (7, 2), (8, 1), (9, 1), (8, 4), (10, 4)] Output : {4: 4, 2: 3, 1: 2} Explanation : 3, 2, 8 and 10 are keys for value 4. Input : test_list = [(3, 4), (1, 2), (8, 1),
6 min read
Python | Iterate through value lists dictionary
While working with dictionary, we can have a case in which we need to iterate through the lists, which are in the keys of dictionaries. This kind of problem can occur in web development domain. Let's discuss certain ways in which this problem can be solved. Method #1: Using list comprehension List c
4 min read
Python Program to Extract Elements from list in set
We are given a list and our task is to extract unique elements from list in a set. For example: a = [1, 2, 3, 4, 5, 2, 3, 6] here we would only extract those elements from list which are unique hence resultant output would be {1,2,3,4,5,6}. Using Set Conversion In this method we convert the list int
2 min read
Python - Filter odd elements from value lists in dictionary
Sometimes, while working with Python dictionaries we can have problem in which we need to perform the removal of odd elements from values list of dictionaries. This can have application in many domains including web development. Lets discuss certain ways in which this task can be performed. Method
6 min read
Python - Unique Values of Key in Dictionary
We are given a list of dictionaries and our task is to extract all the unique values associated with a given key. For example, consider: data = [ {"name": "Aryan", "age": 25}, {"name": "Harsh", "age": 30}, {"name": "Kunal", "age": 22}, {"name": "Aryan", "age": 27}]key = "name" Then, the unique value
4 min read
Python - Extract Unique values dictionary values
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 value
7 min read
Convert List of Tuples to Dictionary Value Lists - Python
The task is to convert a list of tuples into a dictionary where the first element of each tuple serves as the key and the second element becomes the value. If a key appears multiple times in the list, its values should be grouped together in a list. For example, given the list li = [(1, 'gfg'), (1,
4 min read