How do I count the occurrences of a list item in Python?

How do I count the occurrences of a list item in Python?

Answer: To Count the occurrences of items in the list, you can use the count() method in Python.

Lists are Python’s most flexible ordered collection object type. It can also be referred to as a sequence that is an ordered collection of objects. Counting the occurrences of items in a list is essential for tasks like detecting duplicates, performing natural language processing, and enhancing performance. In this article, we will explore how to count the occurrences of an item in a list.

Table of Contents:

Methods to Count the Occurrences of an Item in a Python List

Following are some methods that are used to count the occurrences of and item in a Python list:

Method 1: Use the count() Method to Count the Occurrences of a List Item in Python

The count() method is a built-in method in Python that counts how many times the element appears in a list.

Syntax: 

list.count(element)

Here, the element is an item in which we count the occurrences, and count() counts the no.of occurrences of an element.

Example:

Python

Output: 

Here, rahul appears ‘2’ times in the list, so students.count(“rahul”) returns the no.of occurrences in the list.

Method 2: Using for loop to Count the Occurrences of an Item in a Python List

Using for loop we can manually iterate over the list and keep a count of the number of times the element appears.

Example:

Python

Output:

In this example, we manually increment the count variable every time the item “rahul” is found in the list.

Method 3: Using collections.counter to Count the Occurrences of an Item in a Python List.

The collection module in Python is a part of the standard library that provides specialized data structures and is useful in counting the occurrence of an item in the list.

Syntax:

from collections import Counter
Counter(list) 

Example: 

Python

Output:

Here, Counter(students) creates  a dictionary-like object and student_count[‘rahul’])

returns the count of rahul.

Method 4: Using the list comprehension Method to Count the Occurrences of an Item in a Python List

This list comprehension in Python is a method that is used to create lists. It can also be used to filter the elements from a list and count the occurrences of a specific item.

Syntax: 

[expression for item in iterable if condition]

Here, the expression represents each item that we want to include in the list, and item represents the each element, and iterable is the list(tuple, string, etc) that we are iterating over. This condition filters the elements from the list and includes only if the element meets the condition.

Example:

Python

Output:

The len() function counts the number of items that are present in the list, Here 2 is the count.

Method 5: Using the map() and sum() Methods to Count the Occurrences of an Item in a Python List

These two methods are advanced methods for counting the occurrences of a list by using a map() function that checks if an item matches a specific value, if it matches it returns ‘1’, otherwise it returns ‘0’. Then sum() is to count how many times the item appears and returns the sum.

Example:

Python

Output:

Note: A lambda function in Python is a small, anonymous function. It can take any number of arguments but can only have a single expression. It is defined by using the lambda keyword.

Method 6: Using the enumerate() Method to Count the Occurrences of an Item in a Python List

In Python the enumerate() method doesn’t directly count the number of occurrences, it is only used to loop the list and keep track of the count of the list items.

Example: 

Python

Output: 

Method 7: Using the countOf() Method to Count the Occurrences of an Item in a Python List

In Python, the countOf() method is used to get the count of the number of times the element is present in the list. 

Syntax:

operator.countOf(a,b)

Here, ‘operator.countOf(a,b)’ represents how many times b appears in the iterable a.

Example:

Python

Output: 

Here, the alphabet is the list, and the count of a is 3 because a appears thrice in the list.

Method 8: Using the panda’s Library to Count the Occurrences of an Item in a Python List

In Python, the panda’s library counts the number of occurrences of the elements in the list by converting the list to a pandas series and then using the value_counts() method to count the number of occurrences of each unique element.

Example: 

Python

Output: 

Conclusion

In Python, there are several ways to count the occurrences of an item in a list. The most commonly used methods are count() for the simplest way to count the occurrences of an item, for loops() for getting the custom logic, counter from the collection module for counting multiple items at once, list comprehension method for filter and count the elements and map() with sum() efficient for counting the occurrences, countOf() method for to get the exact occurrences, and lastly Pandas library by converting the list to pandas series and using the value_counts() to count the unique element occurrences.

About the Author

Senior Consultant Analytics & Data Science

Sahil Mattoo, a Senior Software Engineer at Eli Lilly and Company, is an accomplished professional with 14 years of experience in languages such as Java, Python, and JavaScript. Sahil has a strong foundation in system architecture, database management, and API integration. 

Full Stack Developer Course Banner