Count Occurrences of Each Element in List in Python (4 Examples)

 

In this Python article, you will learn how to count the occurrences of each item in a list.

The table of content is structured as follows:

Let’s go straight to the examples!

 

Example Data

For this tutorial, we need to construct data that we can use in the examples. Regarding that, see my_list below.

my_list = ['red', 'blue', 'red', 'green', 'orange', 'blue', 'green', 'yellow', 'purple', 'blue']

As you can see, my_list is a list object that contains ten string elements. Let’s see how to count each one of them!

 

Example 1: Count Occurrences of Each Item in List Using Counter() Constructor

This example illustrates how to get the total number of counts of each item in a list using the Counter() constructor of the collections module. Let’s first import the Counter() constructor as follows.

from collections import Counter

Now, we can apply the Counter() constructor to get the number of times that each element is repeated in our list.

Counter(my_list)
#  Counter({'red': 2,
#           'blue': 3,
#           'green': 2,
#           'orange': 1,
#           'yellow': 1,
#           'purple': 1})

The output above shows the occurrences for each item in my_list: the element ‘blue’ appears three times, the elements ‘red’ and ‘green’ appear two times and the elements ‘orange’, ‘yellow’, and ‘purple’ appear only once.

 

Example 2: Count Occurrences of Each Item in List Using pandas Library

The pandas library can also be useful for counting each item in a list. First, we will import the library.

import pandas as pd

Now, we can convert our list into a pandas series and then, use the value_count() method to extract the number of occurrences for each item in my_list.

pd.Series(my_list).value_counts()
# blue      3
# red       2
# green     2
# orange    1
# yellow    1
# purple    1

The printed counts above are the same as in Example 1, but this time the counts are printed in descending order. Also, the final object is a pandas series, not a container like in Example 1.

 

Example 3: Count Occurrences of Each Item in List Using List Comprehension

In this example, we will show how to use the count method in a list comprehension to return the count of each element in my_list. Consider the code below. We iterate through the elements in our list to get the total count for each item.

[[x,my_list.count(x)] for x in set(my_list)]
#[['yellow', 1],
# ['green', 2],
# ['blue', 3],
# ['purple', 1],
# ['red', 2],
# ['orange', 1]]

We obtain the same counts as in the previous examples, but now the counts and items are stored in the lists of a list.

 

Example 4: Count Occurrences of Each Item in List Using User-defined Function

This example shows how to create a function to count every item in a list. The user-defined counts() function will iterate through our list and store the counts of each element in a dictionary.

def counts(a):
  dic = {}
  for j in a:
    if j in dic:
      dic[j] +=1
    else:
      dic[j] =1
  return dic

Now, we can call our function.

counts(my_list)
# {'red': 2, 'blue': 3, 'green': 2, 'orange': 1, 'yellow': 1, 'purple': 1}

The printed dictionary shows the counts for each element in my_list, which are the same as in previous examples. Well done!

 

Video, Further Resources & Summary

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

 

 

In addition, you may want to have a look at the related programming tutorials on our website:

This post has shown how to count the occurrences of elements in a list in Python. In case you have further questions, you may leave a comment below.

 

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