Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags


How to count the number of occurrences of a list item in Python

Kedar Kodgire


Python provides us with different ways to count occurrences of list items. While certain ways are faster than others, this shot will provide you with a variety of methods that can be used.

As an exercise, try out all the methods in this shot to find the best method for yourself. Let me know in the comments which one you found to be the fastest!

Note: You can find the execution time of the code in the output once you run the code.

1. Count

count is the easiest to use of all methods. This method expects one argument, i.e., the list element whose number of occurrences are counted.



Let’s look at an example.

l = ['a', 'a', 'a', 'a', 'a', 'b', 'b', 'b', 'b', 'c', 'c', 'c', 'd', 'd', 'e']

We can also use this method to get a count of all distinct values at once. For this purpose, we can use list comprehensions.

Comprehensions in Python provide us with a compact and concise way to build new sequences (lists, sets, dictionaries, etc.)

Let’s check out some examples.

l = ['a', 'a', 'a', 'a', 'a', 'b', 'b', 'b', 'b', 'c', 'c', 'c', 'd', 'd', 'e']

print([[x,l.count(x)] for x in set(l)])

print(dict((x,l.count(x)) for x in set(l)))

2. Operator

In this type, you have to import the operator module. From this module, we use the countOf method.

This method takes two arguments and returns an integer as an output.



Let’s look at an example.

l = ['a', 'a', 'a', 'a', 'a', 'b', 'b', 'b', 'b', 'c', 'c', 'c', 'd', 'd', 'e']

import operator as op


3. Counter

Here, you have to import the Counter from the collections module. The counter method is a collection where elements are stored as a dictionary with keys and counts as values.

It takes one argument.



Let’s look at an example of this.

l = ['a', 'a', 'a', 'a', 'a', 'b', 'b', 'b', 'b', 'c', 'c', 'c', 'd', 'd', 'e']

from collections import Counter


c = Counter(l) # for single element

4. pandas

pandas, an open-source data analysis and manipulation tool, is a trendy library. We can also use it to count the number of occurrences of list elements.

We can use the Series.value_counts method from pandas. Since pandas uses series that are one-dimensional arrays with axis labels, we convert our list to series and then use the value_counts method on top of it.

value_counts return the object in descending order. The first element is the most frequently occurring element.



Let’s see an example.

import pandas as pd

l = ['a', 'a', 'a', 'a', 'a', 'b', 'b', 'b', 'b', 'c', 'c', 'c', 'd', 'd', 'e']
my_count = pd.Series(l).value_counts()


#in case if you are looking for a count of a particular element

5. loop and dict

A traditional way to build this functionality is with the use of loops, dictionaries, and conditionals.

First, we build an empty dictionary, and we iterate over the list. Then, we check if the element is available in the dictionary. If it is, then we increase the value by 1. Otherwise, we introduce a new element in the dictionary and assign 1 to it.

l = ['a', 'a', 'a', 'a', 'a', 'b', 'b', 'b', 'b', 'c', 'c', 'c', 'd', 'd', 'e']

def countElement(a):
  g = {}
  for i in a:
    if i in g:
      g[i] +=1
      g[i] =1
  return g


Well, that’s it! I hope you enjoyed this shot, and that it helped you figure the fastest method.

Don’t forget to follow me on Twitter for updates on amazing technology.

Happy learning!



View all Courses

Keep Exploring