A tuple is an ordered group of values written with commas, usually inside parentheses, such as (4, 6, 6). It is immutable, which means that once it is made, nobody can add, remove, or replace its items.
count(value) reads the tuple from the first item to the last and returns an int: how many items are equal to value. If none are, the answer is 0, never an error. Items are compared with ==, so 1, 1.0, and True count as the same value.
tuple.count() is an instance method of the built-in tuple type: call it on a tuple with dot notation, such as rolls.count(6). No import is needed, and it works the same in all Python 3 versions. A tuple has only two methods, count() and index(); there is no append() or remove(), because a tuple can never change.
Syntax
Call it on any tuple and pass the one value you want to count.
python
n = my_tuple.count(value)
Call
What it does
(4, 6, 6, 1).count(6)
Returns 2.
("a", "b").count("c")
Returns 0: no item equals "c", and nothing is raised.
((1, 2), (1, 2), 1).count((1, 2))
Returns 2: each whole (1, 2) item matches, and the lone 1 does not.
The tuple is only read, never changed. list.count() works the same way on a list; reach for the tuple version when the data is fixed, such as a finished game log or a word split into letters. To count every distinct value in one pass, use collections.Counter(my_tuple).
Method signatures
python
tuple.count(value)
# returns: int, how many items == value (0 if none)
# the tuple itself is never changed
Parameters
Parameter
Description
value
Required, and exactly one. Any object; each item is compared with it using ==. my_tuple.count() with no argument raises TypeError, and so does passing two values.
Return Value
Return Value
Description
int
A number from 0 up to len(my_tuple). 0 means the value is not in the tuple at all.
Python Examples
Example 1 — Tally Ten Dice Rolls From a Board Game
The rolls are stored in a tuple because a finished game log should never change. Count the sixes, a face that cannot appear, and then every face from 1 to 6.
python
rolls = (4, 6, 2, 6, 6, 1, 3, 6, 5, 2)
sixes = rolls.count(6)
print(sixes)
print(rolls.count(7))
print(f"{sixes} of {len(rolls)} rolls were a six")
for face in range(1, 7):
print(face, rolls.count(face))
Output
4
0
4 of 10 rolls were a six
1 1
2 2
3 1
4 1
5 1
6 4
How It Works
A log that stays put.rolls.count(6) walks all ten rolls of the tuple and finds sixes at positions 1, 3, 4, and 7, so it returns 4.
An impossible face. A die has no 7, so count(7) simply returns 0 and the program keeps going; no try block is needed.
One call per face.range(1, 7) yields the faces 1 to 6, and the six counts it prints add up to 10, the length of the tuple.
Example 2 — Find the Repeated Letters in a Word
Turn a word into a tuple of letters, count a few of them, and compare count() with the in operator.
python
letters = tuple("bookkeeper")
print(letters)
print(letters.count("e"), letters.count("k"))
print("z" in letters, letters.count("z"))
doubles = [ch for ch in "aeiou" if letters.count(ch) >= 2]
print(doubles)
One letter per item.tuple("bookkeeper") splits the word into ten one-character strings and keeps them in the original order, double letters included.
Yes or no versus how many."z" in letters only answers False, while count("z") gives the number 0; in can stop at the first match, but count() always reads to the end.
Vowels that repeat. The list comprehension keeps a vowel only when its count is 2 or more: "e" appears 3 times and "o" twice, so those two are kept.
Example 3 — Count Repeated Clicks at the Same Screen Point
Each click is an (x, y) tuple inside a bigger tuple. See which items count() treats as equal.
Whole points are compared. Two points are equal only when x equals x and y equals y, so (1.0, 2.0) counts as the same click as (1, 2), giving 3.
Order matters, and no digging.(2, 1) is a different point, so it is counted once; the bare number 1 is never a top-level item of clicks, so its count is 0.
1, 1.0, and True match. These three values are equal under ==, so counting 1 or counting True in flags gives the same answer, 3.
Frequently Asked Questions
It returns 0. count() never raises an error for a missing value, unlike tuple.index(), which raises ValueError.
A tuple is immutable, so methods that would change it, such as append(), remove(), or sort(), do not exist. Only the two read-only methods are left: count() and index().
count() compares with ==, and in Python 1 == 1.0 == True. Equal values are counted even when their types differ, while the string "1" is never equal to the number 1.
Use value in my_tuple for a yes or no answer, because it can stop at the first match. count() always reads the whole tuple, so use it only when you need the number.
Use collections.Counter(my_tuple). It reads the tuple once and maps each distinct value to its count, which is faster than calling count() for every value.