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
# 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
# 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
# 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))
OutputThe 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
# 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))
OutputThe 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
# 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 .
OutputThe 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 - 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 | 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 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 program to find the highest 3 values in a dictionary
Dictionary in Python is an unordered collection of data values, used to store data values like a map, which unlike other Data Types that hold only single value as an element, Dictionary holds key:value pair. Examples: Input : my_dict = {'A': 67, 'B': 23, 'C': 45, 'D': 56, 'E': 12, 'F': 69} Output :
5 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 display keys with same values in a dictionary List
Given a list with all dictionary elements, the task is to write a Python program to extract keys having similar values across all dictionaries. Examples: Input : test_list = [{"Gfg": 5, "is" : 8, "best" : 0}, {"Gfg": 5, "is" : 1, "best" : 0}, {"Gfg": 5, "is" : 0, "best" : 0}] Output : ['Gfg', 'best'
5 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 values
4 min read
Python - Assigning Key values to list elements from Value list Dictionary
Given a List of elements, map them with keys of matching values from a value list. Input : test_list = [4, 6, 3, 5, 3], test_dict = {"Gfg" : [5, 3, 6], "is" : [8, 4]} Output : ['is', 'Gfg', 'Gfg', 'Gfg', 'Gfg'] Explanation : 4 is present in "is" key, hence mapped in new list. Input : test_list = [6,
7 min read
Python - Extract Key's value from Mixed Dictionaries List
Given a list of dictionaries, with each dictionary having different keys, extract value of key K. Input : test_list = [{"Gfg" : 3, "b" : 7}, {"is" : 5, 'a' : 10}, {"Best" : 9, 'c' : 11}], K = 'b' Output : 7 Explanation : Value of b is 7. Input : test_list = [{"Gfg" : 3, "b" : 7}, {"is" : 5, 'a' : 10
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