How to Count the Number of Keys in a Python Dictionary?

In this tutorial, I will explain how to count the number of keys in a Python dictionary. As a developer working on a project for one of my clients in New York, I came across a scenario where I needed to count the number of keys in a dictionary. Let’s explore different methods to accomplish this task with examples.

Count the Number of Keys in a Python Dictionary

Let us learn how to count the number of keys in a Python dictionary.

Read How to Update Values in a Python Dictionary?

Method 1. Use len() function

To count the number of keys in a simple dictionary, you can use the built-in len() function in Python. Here’s an example:

student_grades = {
    "John": 85,
    "Emily": 92,
    "Michael": 78,
    "Jessica": 90
}

total_keys = len(student_grades)
print("Total number of keys:", total_keys)

Output:

Total number of keys: 4

I executed the above example code and added the screenshot below.

Count the Number of Keys in a Python Dictionary

In this example, the dictionary student_grades contains student names as keys and their corresponding grades as values. By passing the dictionary to the len() function, we get the total number of keys in the Python dictionary.

Check out Python Sort Dictionary By Key

Method 2. Use the keys() Method

Another way to count the keys in a dictionary is by using the keys() method. This method returns a view object that contains all the keys of the dictionary. You can then pass this view object to len() to get the count. Here’s an example:

employee_info = {
    "Alice": {"department": "Sales", "salary": 50000},
    "Bob": {"department": "Marketing", "salary": 60000},
    "Claire": {"department": "Engineering", "salary": 75000}
}

total_keys = len(employee_info.keys())
print("Total number of keys:", total_keys)

Output:

Total number of keys: 3

I executed the above example code and added the screenshot below.

How to Count the Number of Keys in a Python Dictionary

In this example, employee_info is a dictionary containing employee names as keys and their department and salary information as nested dictionaries. By calling employee_info.keys() , we get a view object containing all the keys of the outer dictionary. Passing this view object to len() gives us the count of keys.

Read Python Increment Value in Dictionary

Count Keys in a Dictionary of Dictionaries in Python

When dealing with more complex data structures, such as a dictionary of dictionaries, counting the total number of keys requires a different approach. You need to iterate through each nested dictionary and sum up the counts. Here’s an example:

sales_data = {
    "New York": {"Q1": 1000, "Q2": 1200, "Q3": 1100, "Q4": 1500},
    "California": {"Q1": 1500, "Q2": 1800, "Q3": 1700, "Q4": 2000},
    "Texas": {"Q1": 800, "Q2": 900, "Q3": 1000, "Q4": 1200}
}

total_keys = sum(len(state_data) for state_data in sales_data.values())
print("Total number of keys:", total_keys)

Output:

Total number of keys: 12

I executed the above example code and added the screenshot below.

Count Keys in a Dictionary of Dictionaries in Python

In this example, the dictionary sales_data represents quarterly sales data for different states. Each state is key in the outer dictionary, and the corresponding value is another dictionary containing the sales figures for each quarter.

To count the total number of keys, we use a list comprehension along with the sum() function. The list comprehension [len(state_data) for state_data in sales_data.values()] iterates over each nested dictionary using sales_data.values() and calculates the length of each inner dictionary using len(). Finally, sum() adds up all the lengths to give us the total count of keys.

Check out Iterate through Dictionary with multiple values in Python

Conclusion

In this article, I helped to learn how to count the number of keys in a Python dictionary. I discussed mainly two important methods such as using the len() function directly, and the keys() method, for more complex dictionaries, such as dictionaries of dictionaries, you can iterate over the nested dictionaries and sum up the counts using a list comprehension.

You may like to read:

51 Python Programs

51 PYTHON PROGRAMS PDF FREE

Download a FREE PDF (112 Pages) Containing 51 Useful Python Programs.

pyython developer roadmap

Aspiring to be a Python developer?

Download a FREE PDF on how to become a Python developer.

Let’s be friends

Be the first to know about sales and special discounts.