Thursday, December 26, 2024
Google search engine
HomeLanguagesPython – Unique Values of Key in Dictionary

Python – Unique Values of Key in Dictionary

Sometimes, while working with Python dictionaries, we can have a problem in which we need to extract the unique values of a particular key in Dictionary List. This kind of problem in very common in day-day programming and web development domain. Let us discuss certain ways in which this task can be performed.

Input : test_list = [{‘Lazyroar’: 10, ‘for’: 8, ‘best’: 10}, {‘best’: 10}] 

Output : [10] 

Input : test_list = [{‘best’: 11}] 

Output : [11]

Method #1 : Using loop + set() 

The combination of above functions can be used to solve this problem. In this, we extract all the elements of key in loop and then convert the extracted list to set to get unique values. 

Python3




# Python3 code to demonstrate working of
# Unique Values of Key in Dictionary
# Using loop + set()
 
# initializing list
test_list = [{'Gfg': 5, 'is': 6, 'best': 7, 'for': 8, 'Lazyroar': 10},
             {'Gfg': 9, 'is': 4, 'best': 7, 'for': 4, 'Lazyroar': 7},
             {'Gfg': 2, 'is': 10, 'best': 8, 'for': 9, 'Lazyroar': 3}]
 
# printing original list
print("The original list is : " + str(test_list))
 
# initializing key
op_key = 'best'
 
# Unique Values of Key in Dictionary
# Using loop + set()
res = []
for sub in test_list:
    res.append(sub[op_key])
res = list(set(res))
 
# printing result
print("The unique values of key : " + str(res))


Output : 

The original list is : [{‘for’: 8, ‘best’: 7, ‘is’: 6, ‘Gfg’: 5, ‘Lazyroar’: 10}, {‘for’: 4, ‘best’: 7, ‘is’: 4, ‘Gfg’: 9, ‘Lazyroar’: 7}, {‘for’: 9, ‘best’: 8, ‘is’: 10, ‘Gfg’: 2, ‘Lazyroar’: 3}] The unique values of key : [8, 7]

Time Complexity: O(n) where n is the number of elements in the list “test_list”. 

Auxiliary Space: O(n) where n is the number of elements in the list “test_list”. 

Method #2: Using list comprehension This is yet another way to solve this problem. In this, we perform in a similar way as above method but in a shorthand approach. 

Python3




# Python3 code to demonstrate working of
# Unique Values of Key in Dictionary
# Using list comprehension
 
# initializing list
test_list = [{'Gfg': 5, 'is': 6, 'best': 7, 'for': 8, 'Lazyroar': 10},
             {'Gfg': 9, 'is': 4, 'best': 7, 'for': 4, 'Lazyroar': 7},
             {'Gfg': 2, 'is': 10, 'best': 8, 'for': 9, 'Lazyroar': 3}]
 
# printing original list
print("The original list is : " + str(test_list))
 
# initializing key
op_key = 'best'
 
# Unique Values of Key in Dictionary
# Using list comprehension
res = list(set(sub[op_key] for sub in test_list))
 
# printing result
print("The unique values of key : " + str(res))


Output : 

The original list is : [{‘for’: 8, ‘best’: 7, ‘is’: 6, ‘Gfg’: 5, ‘Lazyroar’: 10}, {‘for’: 4, ‘best’: 7, ‘is’: 4, ‘Gfg’: 9, ‘Lazyroar’: 7}, {‘for’: 9, ‘best’: 8, ‘is’: 10, ‘Gfg’: 2, ‘Lazyroar’: 3}] The unique values of key : [8, 7]

Time Complexity: O(n) where n is the number of elements in the dictionary. The list comprehension is used to perform the task and it takes O(n) time.
Auxiliary Space: O(n) additional space of size n is created where n is the number of elements in the dictionary.

Method #3:Using Counter() function

Python3




# Python3 code to demonstrate working of
# Unique Values of Key in Dictionary
from collections import Counter
 
# initializing list
test_list = [{'Gfg': 5, 'is': 6, 'best': 7, 'for': 8, 'Lazyroar': 10},
             {'Gfg': 9, 'is': 4, 'best': 7, 'for': 4, 'Lazyroar': 7},
             {'Gfg': 2, 'is': 10, 'best': 8, 'for': 9, 'Lazyroar': 3}]
 
# printing original list
print("The original list is : " + str(test_list))
 
# initializing key
op_key = 'best'
 
# Unique Values of Key in Dictionary
 
res = []
for sub in test_list:
    res.append(sub[op_key])
 
freq = Counter(res)
res = list(freq.keys())
# printing result
print("The unique values of key : " + str(res))


Output

The original list is : [{'Gfg': 5, 'is': 6, 'best': 7, 'for': 8, 'Lazyroar': 10}, {'Gfg': 9, 'is': 4, 'best': 7, 'for': 4, 'Lazyroar': 7}, {'Gfg': 2, 'is': 10, 'best': 8, 'for': 9, 'Lazyroar': 3}]
The unique values of key : [7, 8]

Auxiliary Space: O(N)

Time Complexity:O(N)

Method 4: Using the map() function and the set() function. 

Approach:

  1. Import the Counter module from the collections library to count the frequency of elements in the list.
  2. Initialize the list of dictionaries containing the data to be processed.
  3. Define the key whose unique values we want to find.
  4. Create a lambda function to extract the value of the desired key from each dictionary.
  5. Apply the lambda function to each dictionary in the list using the map() function.
  6. Store the results in a list.
  7. Count the frequency of each element in the list using the Counter() function.
  8. Extract the keys from the counter object using the keys() method and store them in a list.
  9. Print the list of unique values.

Python3




# Python3 code to demonstrate working of
# Unique Values of Key in Dictionary
 
from collections import Counter
 
# initializing list
test_list = [{'Gfg' : 5, 'is' : 6, 'best' : 7, 'for' : 8, 'Lazyroar' : 10},
             {'Gfg' : 9, 'is' : 4, 'best' : 7, 'for' : 4, 'Lazyroar' :7},
             {'Gfg' : 2, 'is' : 10, 'best' : 8, 'for' : 9, 'Lazyroar' : 3}]
 
# printing original list
print("The original list is : " + str(test_list))
 
# initializing key
op_key = 'best'
 
# Unique Values of Key in Dictionary
 
# lambda function to extract the value of the desired key
extract_value = lambda dictionary: dictionary[op_key]
 
# apply the lambda function to each dictionary in the list using the map() function
results = list(map(extract_value, test_list))
 
# count the frequency of each element in the list using the Counter() function
freq = Counter(results)
 
# extract the keys from the counter object using the keys() method and store them in a list
unique_values = list(freq.keys())
 
# printing result
print("The unique values of key : " + str(unique_values))


Output

The original list is : [{'Gfg': 5, 'is': 6, 'best': 7, 'for': 8, 'Lazyroar': 10}, {'Gfg': 9, 'is': 4, 'best': 7, 'for': 4, 'Lazyroar': 7}, {'Gfg': 2, 'is': 10, 'best': 8, 'for': 9, 'Lazyroar': 3}]
The unique values of key : [7, 8]

Time complexity: The time complexity of this approach is O(n) because it only involves iterating over the list once to extract the values of the desired key and O(k) for counting the frequency of the unique values, where k is the number of unique values of the desired key.

Auxiliary space: The auxiliary space used by this approach is O(n+k), where n is the length of the list and k is the number of unique values of the desired key.

Method #6: Using itertools.chain.from_iterable() and set()

Step-by-step approach:

Import the itertools module
Use the chain.from_iterable() function from itertools to flatten the list of dictionary values corresponding to the given key
Convert the flattened iterable to set to get the unique values for the given key.

Python3




# Python3 code to demonstrate working of
# Unique Values of Key in Dictionary
# Using itertools.chain.from_iterable() and set()
 
# importing itertools module
import itertools
 
# initializing list
test_list = [{'Gfg': 5, 'is': 6, 'best': 7, 'for': 8, 'Lazyroar': 10},
             {'Gfg': 9, 'is': 4, 'best': 7, 'for': 4, 'Lazyroar': 7},
             {'Gfg': 2, 'is': 10, 'best': 8, 'for': 9, 'Lazyroar': 3}]
 
# printing original list
print("The original list is : " + str(test_list))
 
# initializing key
op_key = 'best'
 
# Unique Values of Key in Dictionary
# Using itertools.chain.from_iterable() and set()
 
# use chain.from_iterable to flatten the list of values corresponding to the given key
res_set = set(itertools.chain.from_iterable([sub[op_key]] for sub in test_list))
 
# convert set to list
res = list(res_set)
 
# printing result
print("The unique values of key : " + str(res))


Output

The original list is : [{'Gfg': 5, 'is': 6, 'best': 7, 'for': 8, 'Lazyroar': 10}, {'Gfg': 9, 'is': 4, 'best': 7, 'for': 4, 'Lazyroar': 7}, {'Gfg': 2, 'is': 10, 'best': 8, 'for': 9, 'Lazyroar': 3}]
The unique values of key : [8, 7]

Time complexity: O(n), where n is the total number of values in the list of dictionaries.
Auxiliary space: O(k), where k is the number of unique values corresponding to the given key.

RELATED ARTICLES

Most Popular

Recent Comments