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
# 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))
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: 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
# 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))
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]
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))
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: 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
# 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))
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]
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
# 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))
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]
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))
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: O(n)
Auxiliary Space: O(n)
Similar Reads
Ways to extract all dictionary values | Python
While working with Python dictionaries, there can be cases in which we are just concerned about getting the values list and don't care about keys. This is yet another essential utility and solution to it should be known and discussed. Let's perform this task through certain methods. Method #1 : Usin
3 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 | Extract key-value of dictionary in variables
Sometimes, while working with dictionaries, we can face a problem in which we may have just a singleton dictionary, i.e dictionary with just a single key-value pair, and require to get the pair in separate variables. This kind of problem can come in day-day programming. Let's discuss certain ways in
5 min read
Python | Extract filtered Dictionary Values
While working with Python dictionaries, there can be cases in which we are just concerned about getting the filtered values list and donât care about keys. This is yet another essential utility and solution to it should be known and discussed. Letâs perform this task through certain methods. Method
4 min read
Python - Extract Numerical Dictionary values
Sometimes, while working with Python Dictionaries, we can have a problem in which we need to extract only if a particular key index is numeric value from the dictionaries which are in form of strings. This can be desired in applications in which we require to do preprocessing. Let's discuss certain
5 min read
Extract Dictionary Values as a Python List
To extract dictionary values from a list, we iterate through each dictionary, check for the key's presence, and collect its value. The result is a list of values corresponding to the specified key across all dictionaries.For example, given data = {'a': 1, 'b': 2, 'c': 3}, the output will be [1, 2, 3
3 min read
Python - Dictionaries with Unique Value Lists
Given List of dictionaries with list values, extract unique dictionaries. Input : [{'Gfg': [2, 3], 'is' : [7, 8], 'best' : [10]}, {'Gfg': [2, 3], 'is' : [7, 8], 'best' : [10]}] Output : [{'Gfg': [2, 3], 'is': [7, 8], 'best': [10]}] Explanation : Both are similar dictionaries, and hence 1 is removed.
4 min read
Set from Dictionary Values - Python
The task is to extract unique values from a dictionary and convert them into a set. In Python, sets are unordered collections that automatically eliminate duplicates. The goal is to extract all the values from the dictionary and store them in a set.For example, given a dictionary like d = {'A': 4, '
3 min read
Python - Remove duplicate values across Dictionary Values
Dictionaries often contain lists or sets as their values and sometimes we want to remove duplicate values across all dictionary keys. For example, consider the dictionary d = {'a': [1, 2, 3], 'b': [3, 4, 5], 'c': [5, 6]}. The number 3 appears under both 'a' and 'b', and 5 appears under both 'b' and
3 min read
Get Unique Values from List of Dictionary
We are given a list of dictionaries and our task is to extract the unique values from these dictionaries, this is common when dealing with large datasets or when you need to find unique information from multiple records. For example, if we have the following list of dictionaries: data = [{'name': 'A
4 min read