How to Count Occurrences in a List in Python

Short answer
Use my_list.count(item) to count one item, or collections.Counter(my_list) to count every item in a single pass. Counter(my_list).most_common(3) gives the three most frequent values.

Counting how often something appears answers questions like "how many orders for each product?" or "which word is used most?". For one value the count() method is perfect. For many values it becomes slow, because each call scans the whole list again, and Counter is the better tool.

Both are standard Python, no installation needed. See also the list method reference and the collections module.

Quick facts
One itemmy_list.count(item)
All itemsCounter(my_list) from collections
Most commonCounter(my_list).most_common(n)
Missing itemReturns 0, no error
Time complexitycount() O(n) per call; Counter O(n) once

Count one item with list.count()

list.count() scans the list and returns how many items are equal to the value you give it. If the value is not present, it returns 0.

Python
votes = ["red", "blue", "red", "green", "red"]
print(votes.count("red"))
print(votes.count("pink"))

Count every item at once with collections.Counter

Counter reads the list one time and builds a dictionary-like object mapping each item to its count. Looking up a missing item gives 0. Convert it with dict() if you want a normal dictionary.

Python
from collections import Counter

votes = ["red", "blue", "red", "green", "red"]
counts = Counter(votes)
print(dict(counts))
print(counts["red"])

Find the most common items with most_common()

most_common(n) returns a list of (item, count) pairs ordered from highest to lowest count. Items with equal counts keep the order in which they first appeared in the list.

Python
from collections import Counter

words = ["a", "b", "a", "c", "b", "a"]
print(Counter(words).most_common(2))

Count with a plain dictionary loop

If you cannot import Counter, or want to learn how it works, count with a dictionary and get(). Each item starts at 0 and goes up by one every time you see it.

counts = {}
for item in ["a", "b", "a"]:
    counts[item] = counts.get(item, 0) + 1
print(counts)  # {'a': 2, 'b': 1}

Counting methods compared

MethodCountsBest forRepeated lookups
lst.count(x)One valueA single questionSlow (rescans)
Counter(lst)All valuesFrequency tablesFast
Dictionary loopAll valuesLearning, custom rulesFast
sum(1 for x in lst if cond)Items matching a conditionCounting with a testRescans each time

Examples you can run

Press Run, then edit the code and run it again.

Count one value

count() returns 0 for items that are not in the list.

Count one value
orders = ["pen", "book", "pen", "ink", "pen"]
print(orders.count("pen"))
print(orders.count("glue"))
Expected output3
0

Count everything with Counter

Printing dict(counts) shows a plain dictionary in first-seen order.

Count everything with Counter
from collections import Counter

orders = ["pen", "book", "pen", "ink", "pen"]
counts = Counter(orders)
print(dict(counts))
print(counts["glue"])
Expected output{'pen': 3, 'book': 1, 'ink': 1}
0

Top two most common values

most_common() sorts by count, highest first.

Top two most common values
from collections import Counter

words = "to be or not to be to go".split()
print(Counter(words).most_common(2))
Expected output[('to', 3), ('be', 2)]

Count items that match a condition

A generator expression with sum() counts how many values pass a test.

Count items that match a condition
scores = [45, 82, 67, 91, 38, 76]
passed = sum(1 for s in scores if s >= 50)
print(passed)
Expected output4

Common mistakes

Calling count() inside a loop over a big list

✗ Wrong

data = [1, 2, 1, 3] * 1000
for x in set(data):
    print(x, data.count(x))
No error, but each count() rescans the whole list, so it gets slow as the list grows.

✓ Right

from collections import Counter

data = [1, 2, 1, 3] * 1000
for x, n in Counter(data).items():
    print(x, n)

Build a Counter once and look up the counts.

Passing several values to count()

✗ Wrong

nums = [1, 2, 1, 3]
print(nums.count(1, 3))
TypeError: list.count() takes exactly one argument (2 given)

✓ Right

nums = [1, 2, 1, 3]
print(nums.count(1) + nums.count(3))

count() accepts one value. Use Counter for several or sum the individual counts.

Mixing up case in strings

✗ Wrong

names = ["Asha", "asha", "ASHA"]
print(names.count("asha"))
1 (only the exact match is counted)

✓ Right

names = ["Asha", "asha", "ASHA"]
print([n.lower() for n in names].count("asha"))

count() compares exactly. Normalize with lower() first.

How to Count Occurrences in a List in Python: questions people ask

How do I count occurrences of an element in a list in Python?

Call my_list.count(element). It returns the number of items equal to element, or 0 if there are none.

How do I count all elements in a list at once?

Use collections.Counter(my_list), which returns each item with its count after a single pass through the list.

How do I find the most frequent element in a list?

Use Counter(my_list).most_common(1), which returns a list with one (item, count) pair. Take [0][0] for just the item.

Keep learning

Step-by-step lessons:
Python List MethodsPython collections ModulePython Dictionaries

Related:
Python List count() MethodPython Program to Count Character Frequency in a StringPython Word Count Program: Count Words in a StringHow to Remove Duplicates from a List in PythonPython String count() Method

More in lists, dicts and sets:
How to Remove Duplicates from a List in PythonHow to Sort a Dictionary by Value in PythonHow to Merge Two Dictionaries in PythonHow to Reverse a List in PythonHow to Flatten a List of Lists in PythonHow to Check if a List Is Empty in PythonHow to Get the Last Element of a List in PythonHow to Loop Through a Dictionary in Python See all →