Count Number of Keys in Dictionary Python
Dictionary is a particular data type in python. Dictionary stores unique values by taking different keys and their assigned values. Through this article, we will learn about python dictionary count, including count values, count keys, duplicate count values, count key and value occurrences, and creating a python dictionary with the count.
Python dictionary count
Dictionary count means to have a count of keys and values in a python dictionary. We use a function called len() through which we can count the number of key and value pairs of a dictionary.
If we need the count of key-value pairs, we use the len() function. For example, let us have a look at this code
Example
dict={"apple":5, "bananas":4, "orange":6, "grapes":10, "mango":3}
count=len(dict)
print("the count of this dictionary is:",count)
Output
The count of this dictionary is: 5
In the above code, we created a dictionary of the name dict and have a few keys and their assigned values. To find the count of keys-values of that dictionary, we used the len() function. Once the code is executed, we get a count of key-value pairs presented in that dictionary, giving the count of ‘5’,
Count only dictionary values
You can count only values in a python dictionary using the same len() function, but as this function gives number of pairs, you need a count of only values. Now we use dict.values() method to extract only values from the dictionary. This function is an inbuilt function that is available in the python package.
Example
dict={"apple":5, "bananas":4, "orange":6, "grapes":10, "mango":3}
count=len(dict.values())
print("the count of values in this dictionary is:",count)
Output
the count of values in this dictionary is: 5
In this code, we see a dictionary with the name dict. Here, we are counting only values of that dict by using the function len() and dict.values() method. As the code executes, we get the value count as 5.
Count duplicate values from the dictionary
Here well, see how to count the duplicate values in the python dictionary. So to get the duplicate value count, we use the Counter() method to count the duplicate values in the dictionary.
Example
From collections import Counter
dict={"apple":5, "bananas":3, "orange":5, "grapes":10, "mango":3, "Leche":2}
count=Counter(dict.values())
print("the count of duplicate values in this dictionary are:",count)
output
the count of duplicate values in this dictionary are: Counter({5: 2, 3: 2, 10: 1, 2: 1})
In the code, we created a dictionary name dict and assigned some key-values to it. As we wanted to count the duplicate values, we first imported Counter from collections and then used the command Counter() with dict.values() and printed the output. As the program executes, we get the output of the values and the times it got repeated.
Counting key occurrences in the dictionary
To count the key occurrences in the dictionary, we use Counter () by importing it from collections and chain() by importing it from itertools methods. As we create a dictionary and use these functions. Let us look at an example:
from collections import Counter
from itertools import chain
dict={"apple":5, "bananas":3, "orange":5, "grapes":10, "mango":3, "leche":2}
count=Counter(chain.from_iterable(dict))
print("the count of key occurrences in this dictionary are:",count)
Output:
the count of key occurrences in this dictionary are: Counter({'a': 7, 'e': 5, 'n': 4, 'p': 3, 'g': 3, 'l': 2, 's': 2, 'o': 2, 'r': 2, 'b': 1, 'm': 1, 'c': 1, 'h': 1})
As you can see in the above code, we created a dictionary of name dict with key-values. To count the number of key occurrences, we used the method Counter() and chain() and got the output of all the key occurrences in that dictionary.
Creating a dictionary using count
Here we will see how to create a dictionary using count in python. So to create the dictionary, we have to create a list and assign some elements. And then, we create a variable and use the list comprehension method to use the values from the list. Let us look at an example:
listA = ['apple','apple','mango','mango','orange','orange','orange']
new_list = {m:listA.count(m) for m in listA}
print(new_list)
Output
{'apple': 2, 'mango': 2, 'orange': 3}
Looking at this code, we created a list and appended values to it, then made a new list, used the comprehension method, and printed the new list. After execution, we can find a new dictionary created by the value occurrence from the list.