PyPython example

How to count occurrences in a list in Python

4 min read▶ Runs in an isolated hosted runtimeUpdated Jul 2026

Quick answer

For a single value, use the built-in method: items.count("apple"). To count every value at once, use collections.Counter(items) — it tallies the whole list in one pass.

The right tool depends on how many answers you need. list.count() scans the list once per call, which is perfect for one value and quietly quadratic if you call it for every value. Counter walks the list a single time and hands back every count. Each example runs on this page: hit Run, then edit the code and run it again.

1list.count() for one valueRecommended

Every list has a .count(value) method. It compares with == and returns 0 when the value isn't there — no KeyError, no in guard needed.

count_one.py

Output

Prints 3, 1, then 0. Each call walks the entire list, so it's O(n) — fine once, but calling it inside a loop over set(fruits) makes the whole thing O(n²). That's the classic trap; use Counter instead.

2collections.Counter for every value

Counter is a dict subclass built for tallying. Hand it any iterable and it counts everything in one pass. Missing keys return 0 rather than raising, and .most_common(n) gives you the top n already sorted.

count_all.py

Output

Output: Counter({'apple': 3, 'banana': 2, 'cherry': 1}), then 3, 0, then [('apple', 3), ('banana', 2)]. Items must be hashable — strings, numbers and tuples are fine, lists and dicts are not.

3Counting with a condition

When you're counting matches rather than a specific value, sum a generator of 1s. It reads like the question you're asking and never builds an intermediate list.

count_if.py

Output

Prints 5, 3, 5. The last line is the len([...]) equivalent — same answer, but it materialises a throwaway list first, so prefer sum(1 for ...) on anything large.

4A manual tally

dict.get(key, 0) + 1 is the no-imports version, and it's the pattern to reach for when the count needs custom logic — skipping items, normalising keys, counting into an existing dict. defaultdict(int) does the same thing with less typing.

tally.py

Output

Both print {'apple': 3, 'banana': 2, 'cherry': 1} — dicts keep first-seen insertion order, so the output is stable. This is also the escape hatch for unhashable items: key the dict on something hashable, like str(item) or a tuple of its fields.

5Which should you use?

MethodCountsPasses over the listBest for
items.count(x)One valueOne per callA single known value
Counter(items)EverythingOne, totalFrequencies + most_common()
sum(1 for x in items if ...)MatchesOneA condition, not a value
dict.get(x, 0) + 1 loopEverythingOneNo imports / unhashable items

6Common variation: characters and keys

Counter takes any iterable, not just lists. Pass a string and you get a character frequency table; pass a generator and you can count by a key — here, how many words start with each letter.

count_chars.py

Output

Prints 3, [('l', 3), ('o', 2)], then Counter({'a': 3, 'b': 2, 'c': 1}). Swap word[0] for any expression — len(word), word.lower(), record["status"] — to group by whatever you need.

Frequently asked questions

How do you count occurrences of an item in a Python list?

Call the list method: items.count(value). It returns how many elements compare equal to value, and 0 if the value is absent — no in check or try/except needed.

How do you count every unique item in a list at once?

Use collections.Counter(items). It tallies the whole list in a single pass and behaves like a dict, so counts[value] gives one count (returning 0 for missing keys) and counts.most_common(n) returns the top n as (item, count) pairs.

Is list.count() slow?

One call is O(n) — it scans the whole list. The problem is calling it once per distinct value, which makes the loop O(n²). If you need more than one count, build a Counter instead: it is one pass regardless of how many distinct values there are.

How do you count list items that match a condition?

Use sum(1 for x in items if condition). It counts matches without building an intermediate list; len([x for x in items if condition]) gives the same answer but allocates the list first.