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.

Basic Example

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

This tutorial teaches you the different methods to count occurrences of a list item in python and know when to use the other methods.

Using Count()

The count() method counts 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, 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

The counter from the collections library counts the occurrence of a list item.

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.

To count the occurrences of an item using the counter,

  • Pass the list to the Counter constructor, it returns a dictionary with a count of each item in the list.
  • Check the count of each item by accessing the dictionary using the item name as a key.

Code

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()

The countof() method is available in the operator module. It is a built-in module in python.

  • import it using the statement from operator import countOf
  • Pass the list object to the countOf() method

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

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

The value_counts() method from the Pandas library counts the occurrences of list items.

  • It’ll return a pandas series having a count of unique items in the list.

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 simplest and fastest way if you just want to count the occurrence of a list item.

Additional Resources

Leave a Comment