Count occurrences of an item in List in Python

In this article, we will discuss different ways to count the occurrences of an element in a list in Python.

Table Of Contents

Introduction

Suppose we have a list,

listOfNums = [45, 48, 67, 48, 54, 12, 23, 48, 71, 48, 56, 48]

Element 48 exists in this list 5 times. Let’s see how to find the occurrence count using different techniques.

Get Occurrence count of a value in list using count()

In Python, the list class provides a function count(). It accepts a value as an argument and returns the occurrence count of that value in the list.

For example,

listOfNums = [45, 48, 67, 48, 54, 12, 23, 48, 71, 48, 56, 48]

num = 48

# Get the occurrence count of a value in the list
occurrenceCount = listOfNums.count(num)

print(occurrenceCount)

Output:

5

In this example, we fetched the occurrence count of number 48 in the list.

Get Occurrence count of a value in list using for-loop

We can iterate over all the elements of a list one by one and count them if its value matches with the given element. Let’s see an example, where we will count the occurrences of number 48 in a list. For example,

listOfNums = [45, 48, 67, 48, 54, 12, 23, 48, 71, 48, 56, 48]

num = 48

occurrenceCount = 0

# Get the occurrence count of a value in the list
for value in listOfNums:
    if value == num:
        occurrenceCount += 1

print(occurrenceCount)

Output:

5

Get Occurrence count of a value in list using operator.countof()

We can use the countof() function of operator module in Python to get the occurrence count of a value in list. For example,

import operator as op

listOfNums = [45, 48, 67, 48, 54, 12, 23, 48, 71, 48, 56, 48]

num = 48

# Get the occurrence count of a value in the list
occurrenceCount = op.countOf(listOfNums, num)

print(occurrenceCount)

Output:

5

In this example, we fetched the occurrence count of number 48 in the list.

Get Occurrence count of a value in list using Counter

We can create a Counter object from the list. It accepts a list as argument and internally creates a hashtable, where each unique value from the list is the key and the corresponding value field contains the occurrence count of that element in the list. We can use this to get the occurrence count of a value in list. For example,

from collections import Counter 

listOfNums = [45, 48, 67, 48, 54, 12, 23, 48, 71, 48, 56, 48]

num = 48

counterObj = Counter(listOfNums)

# Get the occurrence count of a value in the list
occurrenceCount = counterObj[num]

print(occurrenceCount)

Output:

5

In this example, we fetched the occurrence count of number 48 in the list.

Get Occurrence count of a value in list using Dictionary

Convert a list to dictionary, where each unique value from the list is the key and the corresponding value field contains the occurrence count of that element in the list. Then we can fetch the value of given element from the dictionary, it will give us the occurrence count of that value in the original list. For example,

listOfNums = [45, 48, 67, 48, 54, 12, 23, 48, 71, 48, 56, 48]

freq = {}

# Create a dictionary from the list
for elem in listOfNums:
    if elem not in freq:
        freq[elem] = 1
    else:
        freq[elem] += 1


num = 48

# Get the occurrence count of a value in the list
occurrenceCount = freq[num]

print(occurrenceCount)

Output:

5

In this example, we fetched the occurrence count of number 48 in the list.

Get Occurrence count of a value in list using Pandas

Convert list to a Pandas Series object. Then using the value_counts() function of Series, get the mapping of unique values of list and thier occurrence counts. Then using that mapping get the occurrence count of given value. For example,

import pandas as pd

listOfNums = [45, 48, 67, 48, 54, 12, 23, 48, 71, 48, 56, 48]

num = 48

# Get the occurrence count of a value in the list
occurrenceCount = pd.Series(listOfNums).value_counts()[num]

print(occurrenceCount)

Output:

5

In this example, we fetched the occurrence count of number 48 in the list.

Summary

We learned different ways to get the occurrence count of a value in a list in Python. Happy Coding.

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top