Python | Get the number of keys with given value N in dictionary
Last Updated :
27 Apr, 2023
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
test_dict = { 'gfg' : 1 , 'is' : 2 , 'best' : 3 , 'for' : 2 , 'CS' : 2 }
print ( "The original dictionary : " + str (test_dict))
N = 2
res = 0
for key in test_dict:
if test_dict[key] = = N:
res = res + 1
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
The time complexity of the program is O(n), where n is the number of elements in the dictionary.
The auxiliary space complexity of the program is O(1), because the program uses a constant amount of additional space (the variable res) to compute the result, regardless of the size of the input dictionary.
Method #2: Using sum() + values()
Python3
test_dict = { 'gfg' : 1 , 'is' : 2 , 'best' : 3 , 'for' : 2 , 'CS' : 2 }
print ( "The original dictionary : " + str (test_dict))
N = 2
res = sum (x = = N 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), where n is the length of the list test_dict
Auxiliary Space: O(n) additional space of size n is created where n is the number of elements in the res list
Method #3: Using Counter() function
Python3
from collections import Counter
test_dict = { 'gfg' : 1 , 'is' : 2 , 'best' : 3 , 'for' : 2 , 'CS' : 2 }
print ( "The original dictionary : " + str (test_dict))
N = 2
freq = Counter(test_dict.values())
res = freq[N]
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
test_dict = { 'gfg' : 1 , 'is' : 2 , 'best' : 3 , 'for' : 2 , 'CS' : 2 }
print ( "The original dictionary : " + str (test_dict))
N = 2
res = len ([key for key in test_dict if test_dict[key] = = N])
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) as it iterates through all the keys of the dictionary.
Auxiliary space: O(n) as it creates a new list to store the keys that have the given value N.
Method #5:Using the reduce and functools module:
Algorithm:
- Initialize the dictionary and value N
- Use reduce function to iterate through the values of the dictionary and accumulate the number of times the value N is found
- Print the count of the number of times N is found
Python3
from functools import reduce
test_dict = { 'gfg' : 1 , 'is' : 2 , 'best' : 3 , 'for' : 2 , 'CS' : 2 }
print ( "The original dictionary : " + str (test_dict))
N = 2
count = reduce ( lambda acc, value: acc + (value = = N), test_dict.values(), 0 )
print ( "Frequency of N is:" , count)
|
Output
The original dictionary : {'gfg': 1, 'is': 2, 'best': 3, 'for': 2, 'CS': 2}
Frequency of N is: 3
Time complexity: O(n), where n is the number of values in the dictionary. This is because the reduce function needs to iterate through all the values in the dictionary to count the number of times the value N appears. The lambda function that is used to check if the current value is equal to N takes constant time.
Auxiliary Space: O(1). This is because the algorithm only uses a few constant-size variables (i.e., test_dict, N, and count) to perform the calculation, regardless of the size of the input. The reduce function doesn’t create any additional data structures, and the lambda function only uses a few constant-size variables (i.e., acc, value, and N).
Share your thoughts in the comments
Please Login to comment...