In this tutorial, we will look at how to count the number of keys in a Python dictionary with the help of some examples.
How to count the number of keys in a dictionary in Python?
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.
Using len()
function
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.
Introductory ⭐
- Harvard University Data Science: Learn R Basics for Data Science
- Standford University Data Science: Introduction to Machine Learning
- UC Davis Data Science: Learn SQL Basics for Data Science
- IBM Data Science: Professional Certificate in Data Science
- IBM Data Analysis: Professional Certificate in Data Analytics
- Google Data Analysis: Professional Certificate in Data Analytics
- IBM Data Science: Professional Certificate in Python Data Science
- IBM Data Engineering Fundamentals: Python Basics for Data Science
Intermediate ⭐⭐⭐
- Harvard University Learning Python for Data Science: Introduction to Data Science with Python
- Harvard University Computer Science Courses: Using Python for Research
- IBM Python Data Science: Visualizing Data with Python
- DeepLearning.AI Data Science and Machine Learning: Deep Learning Specialization
Advanced ⭐⭐⭐⭐⭐
- UC San Diego Data Science: Python for Data Science
- UC San Diego Data Science: Probability and Statistics in Data Science using Python
- Google Data Analysis: Professional Certificate in Advanced Data Analytics
- MIT Statistics and Data Science: Machine Learning with Python - from Linear Models to Deep Learning
- MIT Statistics and Data Science: MicroMasters® Program in Statistics and Data Science
🔎 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.
Using iteration
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 –
- Check If a Python Dictionary Contains a Specific Key
- Check If a Dictionary Is Empty In Python
- 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.