Wednesday, December 25, 2024
Google search engine
HomeLanguagesPython – Unique values count of each Key

Python – Unique values count of each Key

Given a Dictionaries list, the task is to write a Python program to count the unique values of each key.

Example:

Input : test_list = [{"gfg" : 1, "is" : 3, "best": 2}, {"gfg" : 1, "is" : 3, "best" : 6}, 
                    {"gfg" : 7, "is" : 3, "best" : 10}]
Output : {'gfg': 2, 'is': 1, 'best': 3}
Explanation : gfg has 1 and 7 as unique elements, hence 2.
Input : test_list = [{"gfg" : 1, "is" : 3, "best": 2}, {"gfg" : 1, "is" : 3, "best" : 6},
                     {"gfg" : 1, "is" : 3, "best" : 10}]
Output : {'gfg': 1, 'is': 1, 'best': 3}
Explanation : gfg has only 1 as unique element, hence 1.

Method #1 : Using len() + set() + loop

In this, unique values is extracted using set(), len() is used to get its count, and then the result is mapped to each key extracted using keys(). Iterating over keys occurs in a loop.

Python3




# Python3 code to demonstrate working of
# Unique values count of each Key
# Using len() + set()
 
# initializing lists
test_list = [{"gfg": 1, "is": 3, "best": 2}, {
    "gfg": 1, "is": 3, "best": 6}, {"gfg": 7, "is": 3, "best": 10}]
 
# printing original list
print("The original list is : " + str(test_list))
 
res = dict()
for key in test_list[0].keys():
 
    # mapping unique values.
    res[key] = len(set([sub[key] for sub in test_list]))
 
# printing result
print("Unique count of keys : " + str(res))


Output

The original list is : [{'gfg': 1, 'is': 3, 'best': 2}, {'gfg': 1, 'is': 3, 'best': 6}, {'gfg': 7, 'is': 3, 'best': 10}]
Unique count of keys : {'gfg': 2, 'is': 1, 'best': 3}

Time Complexity: O(n*n)
Auxiliary Space: O(n)

Method #2 : Using dictionary comprehension + len() + set()

Similar to the above method, the difference is dictionary comprehension is used as one-liner alternative for shorthand.

Python3




# Python3 code to demonstrate working of
# Unique values count of each Key
# Using len() + set() + dictionary comprehension
 
# Initializing lists
test_list = [{"gfg": 1, "is": 3, "best": 2}, {
    "gfg": 1, "is": 3, "best": 6}, {"gfg": 7, "is": 3, "best": 10}]
 
# Printing original list
print("The original list is : " + str(test_list))
 
# Dictionary comprehension for compact solution
res = {key: len(set([sub[key] for sub in test_list]))
       for key in test_list[0].keys()}
 
# Printing uunique counts of kets as result
print("Unique count of keys : " + str(res))


Output

The original list is : [{'gfg': 1, 'is': 3, 'best': 2}, {'gfg': 1, 'is': 3, 'best': 6}, {'gfg': 7, 'is': 3, 'best': 10}]
Unique count of keys : {'gfg': 2, 'is': 1, 'best': 3}

Time Complexity: O(n) where n is the number of elements in the list “test_list”. The dictionary comprehension + len() + set() is used to perform the task and it takes O(n) time.
Auxiliary Space: O(n), new list of size O(n) is created where n is the number of elements in the list 

Method #3:Using Counter() method

Python3




# Python3 code to demonstrate working of
# Unique values count of each Key
from collections import Counter
 
# initializing lists
test_list = [{"gfg": 1, "is": 3, "best": 2}, {
    "gfg": 1, "is": 3, "best": 6}, {"gfg": 7, "is": 3, "best": 10}]
 
# printing original list
print("The original list is : " + str(test_list))
 
res = dict()
for key in test_list[0].keys():
 
    # mapping unique values.
    res[key] = len(Counter([sub[key] for sub in test_list]))
 
# printing result
print("Unique count of keys : " + str(res))


Output

The original list is : [{'gfg': 1, 'is': 3, 'best': 2}, {'gfg': 1, 'is': 3, 'best': 6}, {'gfg': 7, 'is': 3, 'best': 10}]
Unique count of keys : {'gfg': 2, 'is': 1, 'best': 3}

Time Complexity:O(N*N)
Auxiliary Space: O(N*N)

Method #4: Using for loop

We can use for loop for unique value count in each key. We can traverse each element in the dictionary and check

Here is the step-by-step algorithm for the implementation of the code:

  1. Initialize the list of dictionaries test_list.
  2. Initialize an empty dictionary res to store the result.
  3. Iterate through the keys of the first dictionary in test_list.
  4. For each key, create a temporary list temp_list that contains the values of that key from all the dictionaries in test_list.
  5. Create a set temp_set from the temp_list to get the unique values.
  6. Add the length of temp_set as the value for the corresponding key in the res dictionary.
  7. Print the resulting res dictionary.

Python




# initializing lists
test_list = [{"gfg": 1, "is": 3, "best": 2}, {    "gfg": 1, "is": 3, "best": 6}, {"gfg": 7, "is": 3, "best": 10}]
 
# printing original list
print("The original list is : " + str(test_list))
 
res = dict()
for key in test_list[0].keys():
    temp_list = [sub[key] for sub in test_list]
    temp_set = set()
    for i in temp_list:
        temp_set.add(i)
    res[key] = len(temp_set)
 
# printing result
print("Unique count of keys : " + str(res))
#This code is contributed by Vinay Pinjala.


Output

The original list is : [{'is': 3, 'gfg': 1, 'best': 2}, {'is': 3, 'gfg': 1, 'best': 6}, {'is': 3, 'gfg': 7, 'best': 10}]
Unique count of keys : {'is': 1, 'gfg': 2, 'best': 3}

Time Complexity:O(N*N)
Auxiliary Space: O(N*N)

Method #5: Using numpy

Algorithm:

  1. Iterate through each key in the first dictionary in the list of dictionaries.
  2. For each key, create a numpy array with the values of the corresponding key in each dictionary.
  3. Use the numpy.unique() function to get the unique values of the array.
  4. Get the length of the unique values array and store it as the value for the current key in the result dictionary.
  5. Return the result dictionary.

Python3




import numpy as np
 
# input list
test_list = [{"gfg": 1, "is": 3, "best": 2},
             {"gfg": 1, "is": 3, "best": 6},
             {"gfg": 7, "is": 3, "best": 10}]
 
result = {}
 
# printing original list
print("The original list is : " + str(test_list))
 
# Looking for key in our list
for key in test_list[0]:
    values = np.array([d[key] for d in test_list])
    result[key] = len(np.unique(values))
 
 
# Printing result
print("Unique count of keys : " + str(result))
 
# This code is contributed by Rayudu


Output

The original list is : [{'is': 3, 'gfg': 1, 'best': 2}, {'is': 3, 'gfg': 1, 'best': 6}, {'is': 3, 'gfg': 7, 'best': 10}]
Unique count of keys : {'is': 1, 'gfg': 2, 'best': 3}

Time complexity: O(K)
The algorithm iterates through each key in the first dictionary in the list once, which takes O(k) time, where k is the number of keys in the dictionary.
For each key, the algorithm creates a numpy array with the values of the corresponding key in each dictionary. This takes O(n) time, where n is the number of dictionaries in the list.
The numpy.unique() function takes O(m log m) time, where m is the number of unique values in the array.
Getting the length of the unique values array takes O(1) time.
Therefore, the overall time complexity of the algorithm is O(kn(m log m)).

Auxiliary Space: O(nk + m)
The algorithm creates a numpy array for each key, which takes O(nk) space, where n is the number of dictionaries in the list and k is the number of keys in each dictionary. The numpy.unique() function creates a copy of the array, which takes O(m) space.
The result dictionary takes O(k) space to store the counts of unique values for each key.
Therefore, the overall space complexity of the algorithm is O(nk + m + k), which simplifies to O(nk + m).

Method 6:  use the pandas library

Python3




import pandas as pd
 
# initializing lists
test_list = [{"gfg": 1, "is": 3, "best": 2}, {"gfg": 1, "is": 3, "best": 6}, {"gfg": 7, "is": 3, "best": 10}]
 
# printing original list
print("The original list is : " + str(test_list))
 
# create DataFrame
df = pd.DataFrame(test_list)
 
# get unique count of keys
res = df.nunique().to_dict()
 
# printing result
print("Unique count of keys : " + str(res))


Output

The original list is : [{'is': 3, 'gfg': 1, 'best': 2}, {'is': 3, 'gfg': 1, 'best': 6}, {'is': 3, 'gfg': 7, 'best': 10}]
Unique count of keys : {'is': 1, 'gfg': 2, 'best': 3}

Time complexity: O(n), where n is the total number of elements in the list of dictionaries.
Auxiliary space: O(n), where n is the total number of elements in the list of dictionaries, due to the creation of the pandas DataFrame.

RELATED ARTICLES

Most Popular

Recent Comments