Count Occurrences of Specific Item in List in Python (4 Examples)

 

In this tutorial, you will learn how to count occurrences of a specific element in a list in the Python language.

The article contains four examples, distributed as follows:

Let’s take a look!

 

Example Data

We will use the data below as a basis for this Python tutorial.

my_list = ["car", "bicycle", "car", "metro", "bus", "plane", "bicycle", "car", "bus", "metro"]
print(my_list)
# ['car', 'bicycle', 'car', 'metro', 'bus', 'plane', 'bicycle', 'car', 'bus', 'metro']

As seen in the previously shown output, our example data is a list object that contains ten strings, and some of them appear several times. Let’s see how to count a specific item in my_list!

 

Example 1: Count Frequency of Specific Element in List Using Counter() Constructor

In this example, I will show how to count the number of times that the string ‘car’ is repeated in my_list by using the Counter() constructor from the collections module. First, let’s import the collections module.

from collections import Counter

Now, we can apply the Counter() constructor on our list and get the number of times that ‘car’ is repeated.

Counter(my_list)["car"]
# 3

As shown, the string ‘car’ is repeated 3 times in our list. Great! If you are interested in other alternative solutions, keep reading!

 

Example 2: Count Frequency of Specific Element in List Using Count() Method

The count() method can also be used to count the times that a specific element is repeated in a list. Take a look at the following code.

my_list.count("car")
# 3

As you can see, the count() method counts 3 occurrences of the string ‘car’, as expected.

 

Example 3: Count Frequency of Specific Element in List Using List Comprehension

Alternatively, we can use the list comprehension method to count the occurrences of a specific item in a list. In the code below, see how the list comprehension is set to find the number of times that the string “car” appears in my_list.

len([i for i in my_list if i=='car'])
# 3

The list comprehension method counts 3 occurrences of “car” in my_list, just as supposed to.

 

Example 4: Count Frequency of Specific Element in List Using CountOf() Function

Similar to the Counter() function, we can use the CountOf() function to count the times that an item occurs in a list. To use it, we will first import the operator module.

import operator

Next, we will plug my_list and the ‘car’ string in CountOf().

operator.countOf(my_list,'car')
# 3

Again, as expected, we obtain that the string ‘car’ occurs 3 times in my_list.

 

Video, Further Resources & Summary

Do you need more explanations on how to count the number of times that a specific item appears in a list? Then you should have a look at the following YouTube video of the Statistics Globe YouTube channel.

 

The YouTube video will be added soon.

 

In addition, you might want to read the other tutorials on Statistics Globe. Here you have a selection of other tutorials:

This post has shown how to count the number of times that a certain item is repeated in a list in Python. Please let us know in the comments section below if you have additional questions.

 

Paula Villasante Soriano Statistician & R Programmer

This page was created in collaboration with Paula Villasante Soriano. Please have a look at Paula’s author page to get more information about her academic background and the other articles she has written for Statistics Globe.

 

Subscribe to the Statistics Globe Newsletter

Get regular updates on the latest tutorials, offers & news at Statistics Globe.
I hate spam & you may opt out anytime: Privacy Policy.


Leave a Reply

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

Fill out this field
Fill out this field
Please enter a valid email address.

Top