Count occurrence of an item in List in Python

In this article, we will discuss different ways to count the occurrence of an item in a List in Python.

Table Of Contents

Count occurrence of an item in List using count() method

In Python, the list class provides a member function count(). It accepts an element as an argument and returns the occurrence count of that element in the List. Let’s use this to count the frequency of value 43 in a list,

listofNumbers  = [43, 12, 43, 54, 78, 43, 78]

# Get occurrence count of number 43 in the list
occurrenceCount = listofNumbers.count(43)

print('Occurrence Count : ', occurrenceCount)

Output:

Occurrence Count :  3

The number 43 occurred three times in the List.

Advertisements

Count occurrence of an item in List using for loop

Iterate over all elements of a list using a for loop and count the occurrences of a give element during the loop. For example,

listofNumbers  = [43, 12, 43, 54, 78, 43, 78]

# Get occurrence count of number 43 in the list
occurrenceCount = 0
for elem in listofNumbers:
    if elem == 43:
        occurrenceCount = occurrenceCount + 1

print('Occurrence Count : ', occurrenceCount)

Output:

Occurrence Count :  3

The number 43 occurred three times in the List.

Count occurrence of an item in List using operator

The countOf() function of operator module accepts a sequence and an element as arguments. It returns the number of times given element occurs in the given sequence. We can pass a list and an element as arguments to the countOf() function, and it will return the occurrence count of given element in the given List. For example,

import operator as op

listofNumbers  = [43, 12, 43, 54, 78, 43, 78]

# Get occurrence count of number 43 in the list
occurrenceCount = op.countOf(listofNumbers, 43)

print('Occurrence Count : ', occurrenceCount)

Output:

Occurrence Count :  3

The number 43 occurred three times in the List.

Count occurrence of an item in List using Counter

The collections module in Python, provides a Counter class. It is a dictionary subclass and it accepts a sequence as an argument in its constructor. It keeps the frequency of each element of sequence as a hash table. Therefore, once Counter object is initialized with a sequence, we can get the frequency of any element of sequence from the Counter object. Let’s see how to get the occurrence count of number 43 in a list of numbers,

from collections import Counter

listofNumbers  = [43, 12, 43, 54, 78, 43, 78]

# Get occurrence count of number 43 in the list
counterObj = Counter(listofNumbers)
occurrenceCount = counterObj[43]

print('Occurrence Count : ', occurrenceCount)

Output:

Occurrence Count :  3

The number 43 occurred three times in the List.

Count occurrence of an item in List using Dict

Iterate over all elements of list and store the frequency of each element in a dictionary. Then we can use this dictionary to get the occurrence count of an element in list. For example,

listofNumbers  = [43, 12, 43, 54, 78, 43, 78]

# Calculate frequency of each item in list
freq = {}
for elem in listofNumbers:
    if elem not in freq:
        freq[elem] = 1
    else:
        freq[elem] = freq[elem] + 1

# Get occurrence count of number 43 in the list
occurrenceCount = freq[43]

print('Occurrence Count : ', occurrenceCount)

Output:

Occurrence Count :  3

The number 43 occurred three times in the List.

Count occurrence of an item in List using Pandas

Convert the List to a Pandas Series object and then use the value_count() function to get the frequency of each item in list. Then we can use that to get the occurrence count of an element in list. For example,

import pandas as pd

listofNumbers  = [43, 12, 43, 54, 78, 43, 78]

# Get occurrence count of number 43 in the list
occurrenceCount = pd.Series(listofNumbers).value_counts()[43]

print('Occurrence Count : ', occurrenceCount)

Output:

Occurrence Count :  3

The number 43 occurred three times in the List.

Summary

We learned about different ways to count the occurrences of an item in a List in Python. Thanks.

Pandas Tutorials -Learn Data Analysis with Python

   

Are you looking to make a career in Data Science with Python?

Data Science is the future, and the future is here now. Data Scientists are now the most sought-after professionals today. To become a good Data Scientist or to make a career switch in Data Science one must possess the right skill set. We have curated a list of Best Professional Certificate in Data Science with Python. These courses will teach you the programming tools for Data Science like Pandas, NumPy, Matplotlib, Seaborn and how to use these libraries to implement Machine learning models.

Checkout the Detailed Review of Best Professional Certificate in Data Science with Python.

Remember, Data Science requires a lot of patience, persistence, and practice. So, start learning today.

Join a LinkedIn Community of Python Developers

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