Skip to content
Related Articles
Open in App
Not now

Related Articles

Python – Unique values count of each Key

Improve Article
Save Article
  • Last Updated : 08 Mar, 2023
Improve Article
Save Article

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 occur in 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 above method, difference being 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 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 values 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)


My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!