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 and value is atmost K. 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 code to demonstrate working of # Keys Frequency with Value atmost K # Using loop # Initialize dictionary test_dict = {'gfg' : 1, 'is' : 2, 'best' : 3, 'for' : 4, 'CS' : 5} # printing original dictionary print("The original dictionary : " + str(test_dict)) # Initialize value K = 3 # Using loop # Keys Frequency with Value atmost K res = 0for key in test_dict: if test_dict[key] <= K: res = res + 1 # printing result print("Frequency of keys with values till K is : " + str(res)) |
The original dictionary : {'CS': 5, 'is': 2, 'gfg': 1, 'best': 3, 'for': 4}
Frequency of keys with values till K is : 3
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 code to demonstrate working of # Keys Frequency with Value atmost K # Using sum() + values() # Initialize dictionary test_dict = {'gfg' : 1, 'is' : 2, 'best' : 3, 'for' : 4, 'CS' : 5} # printing original dictionary print("The original dictionary : " + str(test_dict)) # Initialize value K = 3 # Using sum() + values() # Keys Frequency with Value atmost K res = sum(x <= K for x in test_dict.values()) # printing result print("Frequency of keys with values till K is : " + str(res)) |
The original dictionary : {'CS': 5, 'is': 2, 'gfg': 1, 'best': 3, 'for': 4}
Frequency of keys with values till K is : 3
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.
Recommended Posts:
- Maximum length prefix such that frequency of each character is atmost number of characters with minimum frequency
- Python | Delete elements with frequency atmost K
- Python - Extract selective keys' values Including Nested Keys
- Different ways of sorting Dictionary by Keys and Reverse sorting by keys
- Python | Minimum value keys in Dictionary
- Python | Count keys with particular value in dictionary
- Python | Grouping dictionary keys by value
- Python | Initialize common value to keys
- Python | Get the number of keys with given value N in dictionary
- Python | Keys with Maximum value
- Python - Sort Nested keys by Value
- Python - Keys associated with value list in dictionary
- Python - Unique value keys in a dictionary with lists as values
- Python - Merge List value Keys to Matrix
- Python - Rearrange dictionary for consective value-keys
- Python - Multiplication across Like Keys Value list elements
- Python - Minimum value pairing for dictionary keys
- Python - Extract Maximum Keys' value dictionaries
- Python - Remove Keys with K value
- Python - Extract Keys with specific Value Type
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

