Python: Count the Occurrences of Elements in a List (3 Ways)

Updated: June 18, 2023 By: Khue Post a comment

This succinct, example-based article will walk you through a few different ways to count the occurrences of elements in a list in Python. There is no time to waste; let’s dive right in!

Using a dictionary

The main idea of this approach is to use a dictionary to store the counts of each element in the list as key-value pairs, where the keys are the elements and the values are the counts. It works well if all elements in the list are washable.

The steps:

  1. Create an empty dictionary to store the counts.
  2. Loop through the elements in the list and check if they are already in the dictionary. If yes, increment their count by one. If no, add them to the dictionary with a count of one.

Code example:

my_list = ['a', 'b', 'a', 'c', 'd', 'b', 'a', 'c', 'c', 'b', 'a', 'c', 'd']

counts = {}
for element in my_list:
    counts[element] = counts.get(element, 0) + 1

print(counts)

Output:

{'a': 4, 'b': 3, 'c': 4, 'd': 2}

This approach won’t work if your list has an unhashable element like a sublist or a dictionary (a TypeError will be raised).

Using collections.Counter

This is the Pythonic and efficient way to get the task done. We’ll use the Counter class from the collections module to create a dictionary-like object that maps each element in the list to its number of occurrences.

Code example:

# import Counter from collections module
from collections import Counter

# define a list of elements
my_list = ['a', 'b', 'c', 'd', 'd', 'a', 'b', 'b', 'c', 'c', 'c']

# create a Counter object from the list
counts = Counter(my_list)
print(counts)

Output:

Counter({'c': 4, 'b': 3, 'a': 2, 'd': 2})

This technique also has the same drawback as the previous one: does not work if the list contains unhashable elements.

Using the list.count() method (works with nested lists)

In case you want to find the frequency of elements (including unhashable ones) in a list, this is the go-to approach. The list.count(x) method returns the number of times the element x appears in the list (x can be unhashable like a sublist or a dictionary).

The count() method has a time complexity of O(n) for each call, where n is the length of the list.

Example of counting the occurrences of a specific element:

my_list = [1, 2, [3, 4], {'a': 5}, 1, [3, 4], 2, 3, {'a': 5}]

# Count the occurrences of {'a': 5} in my_list
count_1 = my_list.count({'a': 5})
print(count_1)

Output:

2

Example of counting the occurrences of all elements:

my_list = [1, 2, [3, 4], {'a': 5}, 1, [3, 4], 2, 3, {'a': 5}]

# create a new list with unique items from my_list
unique_list = []
for item in my_list:
    if item not in unique_list:
        unique_list.append(item)

# count the number of occurrences of each item in my_list
for item in unique_list:
    count_item = my_list.count(item)
    print(f'"{item}" appears {count_item} times in my_list')

Output:

"1" appears 2 times in my_list
"2" appears 2 times in my_list
"[3, 4]" appears 2 times in my_list
"{'a': 5}" appears 2 times in my_list
"3" appears 1 times in my_list

The overall time complexity of this technique is O(n^2), which can be inefficient for large lists.