Python | Count keys with particular value in dictionary
Last Updated :
15 May, 2023
Sometimes, while working with Python dictionaries, we can come across a problem in which we have a particular value, and we need to find frequency if it’s occurrence. Let’s discuss certain ways in which this problem can be solved.
Method #1: Using loop This problem can be solved using naive method of loop. In this we just iterate through each key in dictionary and when a match is found, the counter is increased.
Python3
test_dict = { 'gfg' : 1 , 'is' : 2 , 'best' : 3 , 'for' : 2 , 'CS' : 2 }
print ( "The original dictionary : " + str (test_dict))
K = 2
res = 0
for key in test_dict:
if test_dict[key] = = K:
res = res + 1
print ( "Frequency of K is : " + str (res))
|
Output
The original dictionary : {'gfg': 1, 'is': 2, 'best': 3, 'for': 2, 'CS': 2}
Frequency of K is : 3
Time Complexity: O(n)
Auxiliary Space: O(1)
Method #2: Using sum() + values() This can also be solved using the combination of sum() and value(). In this, sum is used to perform the summation of values filtered and values of dictionary are extracted using values()
Python3
test_dict = { 'gfg' : 1 , 'is' : 2 , 'best' : 3 , 'for' : 2 , 'CS' : 2 }
print ( "The original dictionary : " + str (test_dict))
K = 2
res = sum (x = = K for x in test_dict.values())
print ( "Frequency of K is : " + str (res))
|
Output
The original dictionary : {'gfg': 1, 'is': 2, 'best': 3, 'for': 2, 'CS': 2}
Frequency of K is : 3
Time Complexity: O(n)
Auxiliary Space: O(1)
Method #3 : Using count() and values().These methods can be used together to find number of keys with particular value.
Python3
test_dict = { 'gfg' : 1 , 'is' : 2 , 'best' : 3 , 'for' : 2 , 'CS' : 2 }
print ( "The original dictionary : " + str (test_dict))
K = 2
list1 = list (test_dict.values())
res = list1.count(K)
print ( "Frequency of K is : " + str (res))
|
Output
The original dictionary : {'gfg': 1, 'is': 2, 'best': 3, 'for': 2, 'CS': 2}
Frequency of K is : 3
Time Complexity: O(n)
Auxiliary Space : O(n)
Method #4: Using lambda functions
Python3
test_dict = { 'gfg' : 1 , 'is' : 2 , 'best' : 3 , 'for' : 2 , 'CS' : 2 }
print ( "The original dictionary : " + str (test_dict))
K = 2
keys = list (test_dict.keys())
res = len ( list ( filter ( lambda x: test_dict[x] = = K, keys)))
print ( "Frequency of K is : " + str (res))
|
Output
The original dictionary : {'gfg': 1, 'is': 2, 'best': 3, 'for': 2, 'CS': 2}
Frequency of K is : 3
Time Complexity: O(n)
Auxiliary Space: O(n)
Method #5: Using collections.Counter()
Python3
import collections
test_dict = { 'gfg' : 1 , 'is' : 2 , 'best' : 3 , 'for' : 2 , 'CS' : 2 }
print ( "The original dictionary : " + str (test_dict))
K = 2
res = collections.Counter(test_dict.values())[K]
print ( "Frequency of K is : " + str (res))
|
Output
The original dictionary : {'gfg': 1, 'is': 2, 'best': 3, 'for': 2, 'CS': 2}
Frequency of K is : 3
Time Complexity: O(n)
Auxiliary Space: O(1)
Method #6: Using operator.countOf() method:
Python3
import operator as op
test_dict = { 'gfg' : 1 , 'is' : 2 , 'best' : 3 , 'for' : 2 , 'CS' : 2 }
print ( "The original dictionary : " + str (test_dict))
K = 2
list1 = list (test_dict.values())
res = op.countOf(list1,K)
print ( "Frequency of K is : " + str (res))
|
Output
The original dictionary : {'gfg': 1, 'is': 2, 'best': 3, 'for': 2, 'CS': 2}
Frequency of K is : 3
Time Complexity: O(N)
Auxiliary Space: O(N)
Method 7: Using a dictionary comprehension
Step-by-step approach:
- Create a new dictionary using a dictionary comprehension that counts the occurrence of values in the original dictionary.
- Create another dictionary using a dictionary comprehension that counts the occurrence of keys with a particular value in the original dictionary.
- Access the value of the key equal to the given value K in the second dictionary to get the frequency
Python3
test_dict = { 'gfg' : 1 , 'is' : 2 , 'best' : 3 , 'for' : 2 , 'CS' : 2 }
K = 2
count_dict = {v: list (test_dict.values()).count(v) for v in set (test_dict.values())}
freq_dict = {v: sum ( 1 for k in test_dict.keys() if test_dict[k] = = v) for v in count_dict.keys()}
res = freq_dict[K]
print ( "Frequency of K is : " + str (res))
|
Output
Frequency of K is : 3
Time complexity: O(n^2)
Auxiliary space: O(n)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...