How to Count Occurrences of a List item in Python – Definitive Guide

Python list allows you to store duplicate items.

You can count occurrences of a list item in Python using the list.count(item) statement.

In this tutorial, you’ll learn how to count occurrences of a list item in python using different methods and know when to use the other methods.

If You’re in Hurry…

You can use the count() method to count occurrences of a list item in Python, as demonstrated below.

Code

num_list = [10, 20,30,30,40,50,50,50,60,60,60,60]

num_list.count(50)

Item 50 occurs thrice in the list. Hence, you’ll see 3 as the output.

Output

    3

If You Want to Understand Details, Read on…

Different methods are available to count the frequency of items in a list in Python. Let us learn about each method and when it is appropriate to use them.

Using Count()

You can use the count() method to count the number of times an item appears in the list.

It returns the number of times an item appears in the list. If the item is not present, then it returns 0.

This is the most commonly used method to count the number of times an item is present in the list.

Code

num_list = [10, 20,30,30,40,50,50,50,60,60,60,60]

num_list.count(50)

Item 50 occurs three times in the list. Hence, you’ll see 3 as output.

Output

    3

Using Counter

You can also use the counter from the collections library to count the occurrence of a list item.

You can use this method when you want to have a count of each item in the list as a dictionary object. Each item will be a key of the dictionary, and item occurrence will be stored as a value of the key.

When you pass the list to the Counter constructor, it returns a dictionary with a count of each item in the list.

You can check the count of each item by accessing the dictionary using the item name as a key.

Code

The below code demonstrates how to count the occurrences of a list item using the counter() method in the collections library.

from collections import Counter

num_list = [10, 20,30,30,40,50,50,50,60,60,60,60]

counts = Counter(num_list)

counts[50]

Since the item 50 occurs thrice in the list, you’ll see the output 3.

Output

    3

Code

You’ll see the output 0 when the passed item is unavailable in the list. Though it is a dictionary, you’ll NOT see the KeyError. This is internally handled by the counter method.

counts[750]

Output

    0

Code

When you print the counts object returned by the counter method, you’ll see a dictionary with the item and count of each item.

counts

Output

    Counter({10: 1, 20: 1, 30: 2, 40: 1, 50: 3, 60: 4})

This is how you can use the collections library to count the occurrence of a list item.

Using Operator Module Countof()

You can also use the countof() method available in the operator module. It is a built-in module in python. You can directly import it using the statement from operator import coountOf. Using this method, you can count values from collections such as tuple, dictionary etc.

You can use this method when you want to count an occurrence of a specific item in the list. For each item, you’ve to invoke the function separately.

Code

The code below demonstrates how to use the countOf() function from the operator module to count the occurrence of an item in the list.

from operator import countOf

num_list = [10, 20,30,30,40,50,50,50,60,60,60,60]

cnt = countOf(num_list, 50)

cnt

Item 50 occurs thrice in the list. Hence, it’ll return 3 as the output.

Output

    3

Code

If the item is not in the list, the countof() method will return 0 as the output.

cnt = countOf(num_list, 750)

cnt

Output

    0

This is how the operator module can be used to count the occurrences of an item in a list.

Using Pandas Value_Counts

You can also use the value_counts() method from the Pandas library to count the occurrences of list items.

It’ll return a pandas series having a count of unique items in the list.
It is appropriate to use this method when you are working with Pandas to manipulate and play around with your data.

Code

import pandas as pd

num_list = [10, 20,30,30,40,50,50,50,60,60,60,60]

count = pd.Series(num_list).value_counts()

count

Pandas series as an output:

60    4
50    3
30    2
10    1
20    1
40    1
dtype: int64

To check the occurrence of a specific item from the list, you can use the below statement.

count[50]

Output

    3

Code

If the item is unavailable in the list, then you’ll see a keyerror exception, as shown below.

count[750]

Output

    ---------------------------------------------------------------------------

    KeyError                                  Traceback (most recent call last)
       3622             except KeyError as err:
    -> 3623                 raise KeyError(key) from err
       3624             except TypeError:
       3625                 # If we have a listlike key, _check_indexing_error will raise


    KeyError: 750

This is how a value_counts() method from the pandas series is used to count the occurrences of a list item.

Count the Number of Occurrences of Each Element in the list

This section teaches you how to count the number of occurrences of each element in the list using:

  • using Collections Counter()
  • Using the Pandas series values_counts() method

Code

from collections import Counter

num_list = [10, 20,30,30,40,50,50,50,60,60,60,60]

Counter(num_list)

Output

    Counter({10: 1, 20: 1, 30: 2, 40: 1, 50: 3, 60: 4})

Code

import pandas as pd

num_list = [10, 20,30,30,40,50,50,50,60,60,60,60]

count = pd.Series(num_list).value_counts()

count

Output

    60    4
    50    3
    30    2
    10    1
    20    1
    40    1
    dtype: int64

Performance

Each method fits based on your needs.

The list count() method is the most simplest and fastest way if you just want to count the occurrence of a list item.

Conclusion

You’ve learned how to count occurrences of a list item in Python using various methods available.

You’ve also learned how to count the occurrence of one single item and how to count the occurrence of each item in the list.

You May Also Like

Leave a Comment