count occurrences of a value in a python dictionary

Count Occurrences of a Value in a Python Dictionary

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.

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.

📚 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.

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 –


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