list.count()
Count how many items in the list compare equal to a value.
Common call
[1, 2, 2, 3].count(2)
Returns
int — 0 when absent
Replaces
uses == equality, so 1 and True count as the same value
Watch out
O(n) every call — counting many values wants Counter
list.count(valuevalue — The value to count. Items match by == equality, not identity.type: Any · required)
→ int
Demo
Live evaluation
Try:
Inputs
listlistcomma-separated items
valueAnyvalue to count
Output
['a', 'b', 'a', 'c', 'a'].count('a')
3
Each item is compared with == against the value; the demo’s comma-separated input makes every item a string. A missing value simply counts as zero — no error.
Parameters
| Name | Type | Required | Description |
|---|---|---|---|
| value | Any | yes | The value to count. Items match by == equality, not identity. |
Return value
int — The number of items equal (==) to value. Zero if none match.
Common patterns
Simple duplicate check
Fine for a single value; use a Counter or set for many.
if names.count(name) > 1: warn("duplicate")
Vote tallying for one option
Direct and readable when only one answer matters.
yes_votes = votes.count("yes")
Examples
1. Count matching items
[1, 2, 2, 3].count(2)
Returns
22. Value not present
["a", "b"].count("z")
Returns
03. Equality, not identity
[1, True, 1.0].count(1)
Returns
3Pitfalls
1. Counting many values is quadratic
count scans the whole list each call.
Slow
{v: lst.count(v) for v in lst}
O(n²) — full scan per item
Fix
from collections import Counter Counter(lst)
O(n) — one pass
2. == equality can surprise
True == 1 and 1.0 == 1, so mixed-type lists count across types.
Surprising
[True, 1, 1.0].count(1)
3
Type-strict count
sum(1 for x in lst if type(x) is int and x == 1)
1
When to use
Use it
- Counting one value in a small-to-medium list
- Quick duplicate existence checks
Reach for something else
- Frequencies of many values → collections.Counter
- Substring counting in a string → str.count
- Just existence → value in lst
Notes
Complexity
O(n) — compares every item
Return
int
CPython impl
Objects/listobject.c :: list_count
Memory
No allocation beyond the result
Thread-safe
Reading is safe; concurrent mutation of the list is not
FAQ
Use sum with a generator expression.
sum(1 for x in lst if x > 10)
History
2.0
Core list method, unchanged semantics since.