In this tutorial, we will look at how to count the occurrences of a value in a Python dictionary with the help of some examples.
Count occurrences of a specific value in a dictionary
Iterate through the dictionary values in a loop and update the counter by one if a match is found to count the occurrences of a specific value in a Python dictionary.
For example, let’s say we have a dictionary storing the names to department mapping of employees in an office. You want to find out how many employees are in the sales department.
# create a dictionary employees = { "Jim": "Sales", "Dwight": "Sales", "Angela": "Accounting", "Toby": "HR" } # count occurrences of "Sales" count = 0 for val in employees.values(): if val == "Sales": count += 1 print(count)
Output:
2
Here we get the count of the value “Sales” in the employees
dictionary. We find that there are two employees in the “Sales” department.
Let’s look at another example.
# create a dictionary employees = { "Jim": "Sales", "Dwight": "Sales", "Angela": "Accounting", "Toby": "HR" } # count occurrences of "Management" count = 0 for val in employees.values(): if val == "Management": count += 1 print(count)
Output:
0
We find that the dictionary employees
does not have any key with the value “Management”. That is, this office does not have any employees in the “Management” department.
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 of each unique value in a dictionary
If you want to count the occurrences of each value in a Python dictionary, you can use the collections.Counter()
function on the dictionary values.
It returns the number of times each value occurs in the dictionary.
For example, let’s find out the number of employees in each department in the above dictionary.
from collections import Counter # create a dictionary employees = { "Jim": "Sales", "Dwight": "Sales", "Angela": "Accounting", "Toby": "HR" } # count of each value in the dictinary count = Counter(employees.values()) print(count)
Output:
Counter({'Sales': 2, 'Accounting': 1, 'HR': 1})
We find that that there are two employees in the “Sales” department, one employee in the “Accounting” department, and one employee in the “HR” department.
For more on collections.Counter()
, refer to its documentation.
Alternatively, you can use an additional dictionary and a loop to get the same result as above.
# create a dictionary employees = { "Jim": "Sales", "Dwight": "Sales", "Angela": "Accounting", "Toby": "HR" } # count of each value in the dictinary count = {} for department in employees.values(): if department in count: count[department] += 1 else: count[department] = 1 print(count)
Output:
{'Sales': 2, 'Accounting': 1, 'HR': 1}
We get the count of each value in the dictionary.
You might also be interested in –
- Python – Count Keys in a Dictionary
- Check If a Python Dictionary Contains a Specific Key
- Python List Count Item Frequency
Subscribe to our newsletter for more informative guides and tutorials.
We do not spam and you can opt out any time.