The world’s leading publication for data science, AI, and ML professionals.

How to Count The Occurrences of a List Item in Python

Understanding how to compute the number of occurrences of a specific list element in Python

Photo by Steve Johnson on Unsplash
Photo by Steve Johnson on Unsplash

Introduction

Counting the number of occurrences of list elements in Python is definitely a fairly common task. In today’s short guide we will explore a few different ways for counting list items. More specifically, we will explore how to

  • count the number of occurrences of a single list item
  • count the occurrences of multiple list items

We will showcase how to achieve both using a specific list method or with the help of collections module.


First, let’s create an example list that we’ll reference throughout this guide in order to demonstrate a few concepts.

>>> my_lst = [1, 1, 2, 3, 1, 5, 6, 2, 3, 3]
>>> my_lst
[1, 1, 2, 3, 1, 5, 6, 2, 3, 3]

Counting the number of occurrences of a single list item

The first option you have when it comes to counting the number of times a particular element appears in a list, is [list.count()](https://docs.python.org/3/tutorial/datastructures.html#more-on-lists) method. For instance, in order to count how many times 1 appears in list my_lst you simply need to run the following command

>>> my_lst.count(1)
3

Alternatively, you can even use the Counter class that comes with the collections module. collections.Counter class is a subclass of dict that is used to count hashable objects. Essentially, it is a collection whether the elements for which counts are observed are stored as dictionary keys and their corresponding counts are stored as dictionary values.

>>> from collections import Counter
>>>
>>> Counter(my_lst)[1]
3

Counting the number of occurrences of multiple list items

Now if you want to count the number of times multiple items appear in a given list then again, you have a couple of options for doing so.

The first option you have is to call list.count() within a for-loop. Let’s say you want to count the number of times that list elements 1, 3 and 5 appear in our list. You can do so, as shown below:

>>> lookup = [1, 3, 5]
>>> [my_lst.count(e) for e in lookup]
[3, 3, 1]

Alternatively, you can use collections.Counter class which in my opinion is more suitable when it comes to counting the number of occurrences of multiple items.

If you want to count the number of times every single element appears in the list then you can simply create an instance of Counter.

>>> from collections import Counter
>>>
>>> Counter(my_lst)
Counter({1: 3, 2: 2, 3: 3, 5: 1, 6: 1})

If you want to count the number of times a subset of elements appear in the list then the following command will do the trick:

>>> from collections import Counter
>>>
>>> lookup = [1, 3, 5]
>>> all_counts = Counter(my_lst)
>>> counts = {k: all_counts[k] for k in lookup}
>>> counts
{1: 3, 3: 3, 5: 1}

Final Thoughts

In today’s short guide we explored a couple of different ways for counting the number of occurrences of list items in Python. We showcased how to use the list method count() or the collections.Counter class in order to count the occurrences of one or multiple list items.


Become a member and read every story on Medium. Your membership fee directly supports me and other writers you read. You’ll also get full access to every story on Medium.

Join Medium with my referral link – Giorgos Myrianthous


You may also like

Use Performance Visualization to Monitor your Python Code


16 Must-Know Bash Commands for Data Scientists


4 Amazingly Useful Additions In Python 3.9


Related Articles