tuple.count()

Tuples have exactly two methods, count and index, because everything else would mutate. This is the counting half.

Tuple methodPython 2.6+Live demo
Common call
t.count(value)
Returns
int — 0 when the value is absent, never an error
Replaces
sum(1 for x in t if x == value)
Watch out
matches by ==, so True counts as 1 and 1.0 counts as 1
tuple.count(valuevalueValue to look for. Compared with ==, so equal-but-not-identical objects still match.type: Any · required)
int

Demo

Live evaluation
Try:
Inputs
itemstupletuple items, comma separated
valueAnyvalue to count
Output
tuple(['a', 'b', 'a']).count('a')
2

count walks the whole tuple and tallies elements equal to the value. There is no early exit — it always looks at every element, because it cannot know there are no more matches ahead. An absent value gives 0 rather than an error, which is the main difference from index. The demo builds the tuple with tuple([...]) so the call preview stays valid Python.

Parameters

NameTypeRequiredDescription
valueAnyyesValue to look for. Compared with ==, so equal-but-not-identical objects still match.

Return value

intNumber of elements equal to value. Returns 0 rather than raising when there are none.

Common patterns

Check for duplicates
More than one occurrence means the value is repeated.
if t.count(value) > 1:
    raise ValueError(f"duplicate {value}")
Tally a fixed record
Tuples often hold fixed rows; counting a flag value summarises them.
passes = row.count("OK")
Count everything at once
Calling count per value is O(n) each time — Counter does it in one pass.
from collections import Counter
tallies = Counter(t)

Examples

1. Appears twice
('a', 'b', 'a').count('a')
Returns
2
2. Appears once
('a', 'b', 'c').count('b')
Returns
1
3. Absent is zero
('a', 'b').count('z')
Returns
0
4. Every element
('x', 'x', 'x').count('x')
Returns
3
5. Empty tuple
().count('a')
Returns
0
6. True equals 1
(1, True, 1.0).count(1)
Returns
3

Pitfalls

1. True, 1 and 1.0 all count as each other
Matching uses ==, and in Python True == 1 == 1.0. A tuple mixing booleans and numbers gives counts that look wrong until you remember that.
Surprising total
(1, True, 1.0).count(1)
3
Compare identity too
sum(1 for x in t if x is 1)
only the literal int
2. Counting each value separately is quadratic
Every call rescans the whole tuple. Counting many distinct values in a loop turns an O(n) job into O(n * k) — use Counter for one pass.
Rescans per value
tallies = {v: t.count(v) for v in set(t)}
O(n * k)
One pass
from collections import Counter
tallies = Counter(t)
O(n)
3. It counts elements, not substrings
Unlike str.count, this compares whole elements. Looking for a fragment inside string elements finds nothing.
No partial match
('abc', 'abd').count('ab')
0
Test each element
sum(1 for s in t if 'ab' in s)
2

When to use

Use it
  • Checking whether a value repeats in a fixed record
  • A single tally over a small tuple
  • Validating that a value appears exactly once
Reach for something else
  • Tallying many values → collections.Counter, one pass
  • You only need presence → the in operator, which short-circuits
  • You want the POSITION → tuple.index

Notes

Complexity
O(n) — always scans the whole tuple, with no early exit
Return
A non-negative int; 0 when the value is absent
CPython impl
Objects/tupleobject.c :: tuplecount
Memory
No allocation — compares in place
Thread-safe
Yes — tuples are immutable

FAQ

Because tuples are immutable, every method that would append, remove, sort or reverse is impossible. What is left are the two questions you can ask without changing anything: how many (count) and where (index).

[m for m in dir(tuple) if not m.startswith("_")]
# ['count', 'index']

History

2.6
tuple.count and tuple.index added, aligning tuple with the Sequence interface.