Skip to content
Related Articles
Open in App
Not now

Related Articles

Python | Get the number of keys with given value N in dictionary

Improve Article
Save Article
  • Last Updated : 19 Jan, 2023
Improve Article
Save Article

In this article, we will see how to get the number of keys with some given value N in a given dictionary. There are multiple methods to do this task. Let’s see them with help of examples. Simple method – 

Python3




# Python3 code to Get the number of keys
# with given value N in dictionary
 
# Initialize dictionary
test_dict = {'gfg' : 1, 'is' : 2, 'best' : 3, 'for' : 2, 'CS' : 2}
 
 
# printing original dictionary
print("The original dictionary : " + str(test_dict))
 
# Initialize value
N = 2
 
# Using loop
# Selective key values in dictionary
res = 0
for key in test_dict:
    if test_dict[key] == N:
        res = res + 1
     
# printing result
print("Frequency of N is : " + str(res))

Output

The original dictionary : {'gfg': 1, 'is': 2, 'best': 3, 'for': 2, 'CS': 2}
Frequency of N is : 3

Method #2: Using sum() + values() 

Python3




# Python3 code to Get the number of keys
# with given value N in dictionary
# Using sum() + values()
 
# Initialize dictionary
test_dict = {'gfg' : 1, 'is' : 2, 'best' : 3, 'for' : 2, 'CS' : 2}
 
# printing original dictionary
print("The original dictionary : " + str(test_dict))
 
# Initialize value
N = 2
 
# Using sum() + values()
# Selective key values in dictionary
res = sum(x == N for x in test_dict.values())
     
# printing result
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

Method #3: Using Counter() function

Python3




# Python3 code to Get the number of keys
# with given value N in dictionary
from collections import Counter
# Initialize dictionary
test_dict = {'gfg': 1, 'is': 2, 'best': 3, 'for': 2, 'CS': 2}
 
 
# printing original dictionary
print("The original dictionary : " + str(test_dict))
 
# Initialize value
N = 2
 
freq = Counter(test_dict.values())
res = freq[N]
 
# printing result
print("Frequency of N is : " + str(res))

Output

The original dictionary : {'gfg': 1, 'is': 2, 'best': 3, 'for': 2, 'CS': 2}
Frequency of N is : 3

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

Method #4: Using list comprehension

This code uses a list comprehension to iterate through the keys in the dictionary, and for each key it checks if the value associated with that key is equal to the given value N. If it is, it adds that key to a new list. The length of this list is then calculated, which represents the number of keys in the dictionary that have a value of N.

Python3




# Method #4: Using list comprehension
 
# Initialize dictionary
test_dict = {'gfg': 1, 'is': 2, 'best': 3, 'for': 2, 'CS': 2}
 
# printing original dictionary
print("The original dictionary : " + str(test_dict))
 
# Initialize value
N = 2
 
# Using list comprehension
# Selective key values in dictionary
res = len([key for key in test_dict if test_dict[key] == N])
 
# printing result
print("Frequency of N is : " + str(res))
 
#This code is contributed by Edula Vinay Kumar Reddy

Output

The original dictionary : {'gfg': 1, 'is': 2, 'best': 3, 'for': 2, 'CS': 2}
Frequency of N is : 3

This method has a time complexity of O(n) as it iterates through all the keys of the dictionary. The auxiliary space of this method is O(n) as it creates a new list to store the keys that have the given value N.


My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!