Python Dictionary – Count Items, Keys and Values

Dictionary is a Python collection used to hold data as a key:value pairs, in some programming languages also known as a map. Keys are unique, acting as an index over the values they hold. In this post, we’ll see how to count items, keys, and values inside a Python dictionary.

The most convenient method to count how many pairs the Python dictionary contains is by using the len() method. Result is a number of distinct keys inside a dictionary, therefore this number also represents how many key:value pairs there are.

Built-in items(), keys(), and values() methods

Python dictionary has some handful built-in methods that can help you look up or count different types of values:

  • dict.items() – returns a list that contains tuples representing key:value pairs.
  • dict.keys() – returns all dictionary keys as a list.
  • dict.values() – returns all dictionary values as a list.

Let’s see these methods in action.

>>> vehicles = {'vans': 2, 'cars': 12, 'buses': 3}

>>> vehicles.items()
dict_items([('vans', 2), ('cars', 12), ('buses', 3)])

>>> vehicles.keys()
dict_keys(['vans', 'cars', 'buses'])

>>> vehicles.values()
dict_values([2, 12, 3])

How to count Python dictionary keys

There are multiple ways how to count keys in a Python dictionary. I will show you three ways:

  • Using keys() and len() method
  • Using len() method
  • Using custom function

Use keys() and len() to count dictionary items

Since keys() method already returns all distinct dictionary keys, you can use it in combination with the len() method to count different pairs inside a Python dictionary. 

As you can see in the following example, this method returns a correct number of keys in dictionary.

>>> vehicles = {'vans': 2, 'cars': 12, 'buses': 3}

>>> len(vehicles.keys())

Use len() to count dictionary items

Although the above example is working, you could write it in shorter way by using only the len() method. The len() method returns the total length of a dictionary, or more precisely, the total number of items in the dictionary.

>>> vehicles = {'vans': 2, 'cars': 12, 'buses': 3}

>>> len(vehicles)
NOTE - Using len() with keys() to count dictionary keys is slower than using len() only; therefore my recommendation is to use the latter.

Use custom function to count dictionary items

If you are in a need to write a custom function that will count a number of dictionary items, take a look at the following example. We used a simple for loop that iterates over tuples returned by the items() method. Each iteration increases the count value by 1. Here you can also add some additional logic if you need to.

def count_items(dictionary):
  count = 0
  for key, value in dictionary.items():
    # Additional logic could go here
    count += 1
  return count

vehicles = {'vans': 2, 'cars': 12, 'buses': 3}
count_items(vehicles)  # 3

Bonus – Python dictionary count duplicate values

As a bonus to this post, let’s see how to count the number of occurrences of each value inside a Python dictionary. To make it easier to understand, let’s introduce the following example. We decided to survey 10 people, with the possibility of answering “yes” or “no”. We store each answer in a dictionary right under that user’s name. The dictionary items look like this:

>>> answers = {'user1': 'yes', 'user2': 'no', 'user3': 'yes', 'user4': 'yes', 'user5': 'yes', 'user6': 'no', 'user7': 'yes', 'user8': 'no', 'user9': 'yes', 'user10': 'no'}

Now, to count how many times each answer was given, we can help ourselves with Python’s built-in collections module. More precisely, we need a Counter class from the collections module. If we pass a list of values to the Counter, as a result we’ll get key:value pairs that will represent each value and its number of occurrences. 

In our example, we get a number of occurrences of each answer in a survey.

>>> from collections import Counter
>>> Counter(answers.values())
Counter({'yes': 6, 'no': 4})