Python Dictionary Count + Examples

In this Python tutorial, we will study Python dictionary Count using some examples in python. Moreover, we will also cover these topics.

  • Python dictionary count values
  • Python dictionary count duplicate values
  • Python dictionary count values per key
  • Python dictionary count key occurrences
  • Python dictionary count value occurrences
  • Python dictionary count keys
  • Python dictionary count frequency
  • Python dictionary count unique values
  • Python create dictionary with count
  • Python count items in dictionary with condition
  • Python dictionary update count
  • Python dictionary count items in list
  • Python count words in dictionary
  • Python sort dictionary count

Python dictionary count

  • In this section, we will learn how to count the keys and values in a Python dictionary.
  • To perform this particular task we are going to use the len() function, which returns the total number of key-value pairs in the dictionary. This method will check the size of items in Python.
  • If you want to find the count number of items stored in a dictionary then you have to use the len() function where items are to be represented as key-value pairs.

Syntax:

Let’s have a look at the syntax and understand the working of the Python len() function

len(dict)
  • dict: This parameter indicates the dictionary whose length is to be calculated

Example:

Let’s take an example and check how to count the items in the Python dictionary

my_dictionary = {'U.S.A':167,'United Kingdom':67,'Japan':167,'China':783}

result=len(my_dictionary)
print("Count dictionary items:",result)

In the above example, we have created a simple dictionary named ‘my_dictionary’ that contains items in the form of key-value pairs. After that, we have used the len() function and within this function, we assigned the dictionary. Once you will execute ‘result’ then the output will display the count of dictionary items ‘4’.

Here is the Screenshot of the following given code.

Python dictionary count keys and values
Python dictionary count keys and values

Read: Get First Key in dictionary Python

Python dictionary count values

  • In this Program, we will understand how to count the values in Python dictionary.
  • By using the len function we can easily count the number of key-value pairs in the dictionary. Now in this example, we have to extract only values from the dictionary.
  • To do this task we are going to use the dict.values method. This method will help the user to get all the values from the given dictionary. This is an inbuilt() method and is available in the Python package.

Syntax:

Here is the Syntax of Python dict.values() method.

dictionary.values()

Note: This function does not take any arguments

Example:

Let’s have a look at the example and check how to use the dict.values() function in Python.

Source Code:

Country_name = {'Germany':456,'U.S.A':789,'Malaysia':745}

new_output=len(Country_name.values())
print("Count values in dictionary:",new_output)
  • In the following given code, we have created a dictionary named ‘Country_name’ that contains elements in the form of key-value pairs.
  • After that, we declared a variable ‘new_output’ in which we have assigned the len() function along with dict.values() method.
  • Once you will execute ‘new_output’ then the result will display the count values in the dictionary.

Here is the implementation of the following given code.

Python dictionary count values
Python dictionary count values

Read: Python dictionary increment value

Python dictionary count duplicate values

  • In this section we learn how to count duplicate values in Python dictionary.
  • By using the counter() method we can easily count the duplicate values in the dictionary. This method is a part of a collection module and it is used for counting the hashable objects.

Example:

from collections import Counter

my_dict = {"John": 156, "U.S.A": 156, "France": 987,
         "George":678, "Australia": 156}

new_output = Counter(my_dict.values())
print("Count duplicate values:",new_output)

In the following given code, we have used the counter() method and within this function, we assigned the dict.values() as an argument. Once you will execute this code it will count the duplicate values which are present in the dictionary.

Here is the Screenshot of the following given code.

Python dictionary count duplicate values
Python dictionary count duplicate values

Read: Python dictionary of lists

Python dictionary count values per key

  • Here we can see how to count the values in dictionary as per the key element.
  • To do this task we are going to use the dictionary.items() method. In Python the dictionary.items() method is used to return a list of tuples that contains dictionary elements in the form of key-value pair.

Syntax:

Let’s have a look at the Syntax and understand the working of dict.items() method.

dictionary.items()

Example:

Let’s take an example and check how to count the values in the dictionary as per key.

Source Code:

new_dictionary = {
    'France ': [56,23,45],
    'U.S.A ': [178,234,567],
    'United Kingdom ': [345,189,678,987],
    'China ': [134,2345,8954,2678,33],
}
for new_k, new_val in new_dictionary.items():
    print(new_k, len([item for item in new_val if item]))

Here is the implementation of the following given code.

Python dictionary count values per key
Python dictionary count values per key

Read: Python dictionary extend – Complete tutorial

Python dictionary count key occurrences

  • Let us see how to count keys occurrences in Python Dictionary.
  • To perform this particular task we are going to use the counter() and chain() methods. First, we will create a dictionary and then we are going to use the counter() function and within this method, we assigned the dictionary as an argument.

Example:

from collections import Counter
from itertools import chain

my_dictionary = [
    {16: 'George', 18: 'Oliva', 78: 'Lilly', 16: 'William'},
    {16: 'John', 16: 'James'},
    {78: 'Micheal', 18: 'Potter'},
]
new_output = Counter(chain.from_iterable(my_dictionary))
print("Count occurrence keys:",new_output)

Here is the implementation of the following given code.

Python dictionary count key occurrences
Python dictionary count key occurrences

As you can see in the Screenshot the output displays the count of occurrence keys.

Read: Python Dictionary Copy with Examples

Python dictionary count value occurrences

  • In this section, we will learn how to count values occurrences in Python dictionary.
  • To do this task we are going to use the counter() module. In Python, the counter is used to count values of an iterable object in python and the values can be zero, positive, negative integers.
  • In this example, we will initialize a list and then use it to group values and occurrence each value.

Example:

Let’s take an example and check how to count value occurrences in a Python dictionary.

Source Code:

from collections import Counter
  
my_new_dict = [{'George' : 167, 'John' : 267}, {'George' : 167, 'John' : 267}, 
             {'George' : 167, 'John' : 267}, {'George' : 167, 'John' : 267},
             {'George' : 167, 'John' : 267}, {'George' : 167, 'John' : 267} ]
 
new_k = 'John'
new_output = dict(Counter(sub[new_k] for sub in my_new_dict))
print(new_output)

You can refer to the below Screenshot.

Python dictionary count value occurrences
Python dictionary count value occurrences

Read: Python dictionary multiple keys

Python dictionary count keys

  • Let us see how to count the key elements in the Python dictionary.
  • By using the len() function we can easily count the number of key-value pairs in the dictionary. Now in this example, we have to extract only key elements from the dictionary.
  • To perform this particular we are going to use the dictionary.keys() method. In Python the dictionary.keys() method returns an object that contains a list of all keys which is available in a given dictionary.

Syntax:

Let’s have a look at the Syntax and understand the working of the dictionary.keys() method.

dict.keys()

Note: This method does not take any argument.

Example:

new_dictionary = {'John':167,'William':267,'Micheal':745,'Chris':134}

new_result=len(new_dictionary.keys())
print("Count keys in dictionary:",new_result)

In the above code, we have created a dictionary named ‘new_dictionary’ in which we assigned the elements in the form of key-value pairs. After that, we have used the dictionary.keys() method for displaying the count of keys element by using the len() function

You can refer to the below screenshot.

How to count keys in Python dictionary
Python dictionary count keys

Read: Python find max value in a dictionary

Python dictionary count frequency

  • In this section, we will learn how to count item frequency in Python dictionary.
  • In this example, we have a given list with multiple values and we have to count the frequency of each value in the list by using the .items() method and it will check the condition of how many occurrences of each element are available in the list.
  • To do this task, we are going to use the list.count() method and it will iterate over the list and count the frequency. In Python list.count() is an in-built() function and it will return the number of times objects or elements in a list.

Syntax:

Let’s have a look at the syntax and understand the working Python list.count() method

list.count(element)

Note: The element parameter indicates the element to be counted in the given list.

Example:

Let’s take an example and check how to count item frequency in Python dictionary

Source Code:

new_list =[16,16,17,17,17,18,18,19,19,19]

frequency = {}
for items in new_list:
   frequency[items] = new_list.count(items)
for new_k, new_val in frequency.items():
   print("Count frequency:",frequency)

Here is the Screenshot of the following given code.

Python dictionary count frequency
Python dictionary count frequency

Read: How to create an empty Python dictionary

Python dictionary count unique values

  • In this section, we will learn how to count unique values in a Python dictionary.
  • In this example, we have used the itertools module along with the chain function. This function takes multiple iterables and always returns one iterable. This method is available in the Python package module.

Syntax:

Here is the Syntax of the chain() function

chain(*iterables)

Example:

from collections import Counter
from itertools import chain

new_list = [{"Germany":167, "U.S.A": 144},
     {"Germany": 456, "U.S.A": 987},
     {"Germany": 98, "China": 77},
    {"Japan":42, "Australia":567}]
new_output=Counter(chain.from_iterable(e.values() for e in new_list))
print(new_output)

Here is the implementation of the following given code.

Python dictionary count unique values
Python dictionary count unique values

Read: Python Dictionary to CSV

Python create dictionary with count

  • Let us see how to create a dictionary with count in Python dictionary.
  • To perform this particular task first we will create a list named ‘new_list’ and then assign multiple elements.
  • Next, we will declare a variable ‘new_result’ and use the list comprehension method to iterate the values from a given list.

Example:

new_list = ['U.S.A','U.S.A','Japan','Japan','Japan','Germany','Germany']

new_result = {m:new_list.count(m) for m in new_list}
print(new_result)

Here is the Screenshot of the following given code

How to create a dictionary with count in Python
Python create a dictionary with a count

Read: Python dictionary of tuples

Python count items in dictionary with condition

  • Here we can see how to count items in Python dictionary with condition.
  • In this Program, we have created a function ‘count_items’ to calculate the number of items in the Python dictionary and then we have declared a variable ‘z’ to 0 and iterated over a dictionary by using the enumerator method in for loop.
  • In Python enumerator() methods return an enumerated object and it inserts a counter to each element of the iterable objects.

Syntax:

Let’s have a look at the syntax and understand the working of enumerator() python

enumerator(iterable,startindex)

Example:

Let’s take an example and check how to count items in a Python dictionary with conditions.

my_dictionary = {'George':17,'Micheal':15,'William':178,'John':432}

def count_items(my_dictionary):
    z = 0
    for i in enumerate(my_dictionary):
        z += 1
    return z

print("Count items in dictionary:",count_items(my_dictionary))

Here is the implementation of the following given code.

Python count items in dictionary with condition
Python count items in a dictionary with condition

As you can see in the Screenshot the output displays the count items in the dictionary is 4.

Read: Python dictionary pop

Python dictionary update count

  • In this section, we will learn how to update count in Python dictionary.
  • To perform this particular task first we will create a dictionary and update a new element by using the dictionary.update() function. After updating the dictionary elements we have to use the len() function which returns the total number of key-value pairs in the dictionary.
  • To get more information regarding Python dictionary update. You can refer our detailed article on update dictionary.

Syntax:

Here is the Syntax of len() function

len(dict)

Example:

my_dictionary = {'U.S.A':167,'United Kingdom':67,'Japan':167,'China':783}
new_update = {'Germany':48}

my_dictionary.update(new_update)
print("Updated Dictionary:",my_dictionary)
result=len(my_dictionary)
print("Count dictionary items:",result)

Here is the execution of the following given code.

Python dictionary update count
Python dictionary update count

Read: Python dictionary find a key by value

Python dictionary count items in list

  • In this program, we will learn how to count items in list by using a Python dictionary.
  • To do this task we are going to use the list comprehension method and iterate all the values which are present in the list.
  • Next in the list comprehension method, we will use the list.count() method and this method will help the user to return the number of times objects or elements in a list.

Example:

list = ['George','George','James','James','James','Micheal','Micheal']

new_output = {z:list.count(z) for z in list}
print("Count item in list:",new_output)

Here is the Output of the following given code.

Python dictionary count items in list
Python dictionary count items in a list

Read: Python dictionary remove

Python count words in dictionary

  • In this section, we will learn how to count the words in a Python dictionary.
  • To perform this particular task first we will create a dictionary and assign elements in the form of key-value pairs. Now we have to get count words from a given dictionary.
  • By using the len() function, we can easily get the count words of keys and values. But in this function, we will use the dict.keys() and dict.values() method. This method will help the user to get all the values and keys from the given dictionary.

Example:

my_dictionary = {'John':456,'Micheal':890,'George':156,'Oliva':789}

result=len(my_dictionary.keys())
result2=len(my_dictionary.values())
print("Count dictionary words keys:",result)
print("Count dictionary words values:",result2)

Here is the execution of the following given code.

Python count words in dictionary
Python count words in a dictionary

Python sort dictionary count

  • In Python sorted() is the built-in function that can easily help the user to sort all the elements in a Python dictionary.
  • To get all the information regarding sorted() function. You can refer our detailed article Python dictionary sort.

Example:

Let’s have a look at the example and understand the working of the sorted() function

Source Code:

my_dictionary = {"Austraila":6542,"United Kingdom":123,"U.S.A":98245,"France":1456} 
 
new_output = sorted(my_dictionary.values())
print("Sorted Values",new_output)  

In the above code, we have just created a simple dictionary named ‘my_dictionary’ that contains a key-value pair element.

After that, we have used the sorted() function and within this function, we assigned the dict.values() method. Once you will execute this code the output displays sorted values.

Here is the Screenshot of the following given code.

Python sort dictionary count
Python sort dictionary count

You may also like to read the following Python tutorials.

In this Python tutorial, we have learned how to count items in a dictionary using some examples in python. Moreover, we will also cover these topics.

  • Python dictionary count values
  • Python dictionary count duplicate values
  • Python dictionary count values per key
  • Python dictionary count key occurrences
  • Python dictionary count value occurrences
  • Python dictionary count keys
  • Python dictionary count frequency
  • Python dictionary count unique values
  • Python create dictionary with count
  • Python count items in dictionary with condition
  • Python dictionary update count
  • Python dictionary count items in list
  • Python count words in dictionary
  • Python sort dictionary count