Count of keys in a python dictionary

Python – Count Keys in a Dictionary

In this tutorial, we will look at how to count the number of keys in a Python dictionary with the help of some examples.

Count of keys in a python dictionary

You can use the Python built-in len() function to count the number of keys in a dictionary. Pass the dictionary as an argument. The following is the syntax:

# count keys in dictionary d
len(d)

It returns the count of keys in the dictionary, which is the same as the length of the dictionary.

Let’s look at some examples.

You can apply the len() function directly on the dictionary to get the number of keys in the dictionary.

# create a dictionary
employees = {
    "Jim": "Sales",
    "Dwight": "Sales",
    "Angela": "Accounting"
}
# count keys in the dictionary
print(len(employees))

Output:

3

We find that the dictionary in this example has three keys, which is correct.

You can get the keys in a dictionary as an iterable using the dictionary’s keys() method. If you apply the len() function to the resulting iterable (which is a dict_keys object), you can get the count of the keys in the dictionary.

📚 Data Science Programs By Skill Level

Introductory

Intermediate ⭐⭐⭐

Advanced ⭐⭐⭐⭐⭐

🔎 Find Data Science Programs 👨‍💻 111,889 already enrolled

Disclaimer: Data Science Parichay is reader supported. When you purchase a course through a link on this site, we may earn a small commission at no additional cost to you. Earned commissions help support this website and its team of writers.

# count keys in the dictionary
print(len(employees.keys()))

Output:

3

We get the same result as above.

A naive way of counting the keys in a dictionary could be to iterate through the dictionary and count its keys using an additional variable.

Here’s an example.

# create a dictionary
employees = {
    "Jim": "Sales",
    "Dwight": "Sales",
    "Angela": "Accounting"
}
# count keys in the dictionary
count = 0
for key in employees:
    count += 1
print(count)

Output:

3

Here we iterate over each key in the dictionary and increase the counter by one in each iteration. We get the same result as the above methods.

In this tutorial, we looked at some methods to get a count of keys in a dictionary. Using the len() function is simple and straightforward compared to the iteration method.

You might also be interested in –

  1. Check If a Python Dictionary Contains a Specific Key
  2. Check If a Dictionary Is Empty In Python
  3. Python view dictionary Keys and Values


Subscribe to our newsletter for more informative guides and tutorials.
We do not spam and you can opt out any time.


Author

  • Piyush Raj

    Piyush is a data professional passionate about using data to understand things better and make informed decisions. He has experience working as a Data Scientist in the consulting domain and holds an engineering degree from IIT Roorkee. His hobbies include watching cricket, reading, and working on side projects.

Scroll to Top