tuple.count()

tuple.count(x)
Returns: int · Updated March 14, 2026 · Tuple Methods
tuples methods searching immutable

The .count() method returns the number of times a specified value appears in a tuple. It scans the tuple linearly and counts all occurrences of the given element, returning an integer. Since tuples are immutable, .count() does not modify the original tuple — it only reads and computes the count.

This method is useful for frequency analysis, validation, and checking how many times a particular element appears in fixed data collections.

Syntax

tuple.count(x)

Parameters

ParameterTypeDefaultDescription
xanyThe value to search for. Can be of any type: int, str, float, list, dict, object, etc. Comparisons use ==.

Return Value

Returns an int representing how many times x appears in the tuple. Always returns 0 or a positive integer — never negative or a float. The original tuple remains unchanged.

Examples

Basic usage - counting numbers

Count how many times a number appears in a tuple:

numbers = (1, 2, 3, 2, 4, 2, 5, 2)
twos = numbers.count(2)
print(twos)
# 4

Counting strings

Count string occurrences in a tuple of words:

fruits = ("apple", "banana", "apple", "cherry", "apple")
apples = fruits.count("apple")
print(apples)
# 3

Counting in a tuple with mixed types

Python tuples can contain mixed types:

mixed = (1, "hello", 3.14, 1, 1, "hello")
print(mixed.count(1))
# 3
print(mixed.count("hello"))
# 2
print(mixed.count(3.14))
# 1

Counting in an empty tuple

Calling .count() on an empty tuple always returns 0:

empty = ()
print(empty.count("anything"))
# 0

Counting nested lists

When counting lists inside a tuple, Python compares by value:

nested = ([1, 2], [3, 4], [1, 2], [5, 6])
print(nested.count([1, 2]))
# 2

Demonstrating immutability

The .count() method does not modify the original tuple:

tup = (1, 2, 3, 2, 4)
result = tup.count(2)
print(result)
# 2
print(tup)
# (1, 2, 3, 2, 4)  # Original unchanged

Common Patterns

Frequency analysis

A common pattern is building a frequency dictionary:

data = ("apple", "banana", "apple", "cherry", "banana", "apple")
unique = set(data)
frequencies = {item: data.count(item) for item in unique}
print(frequencies)
# {"apple": 3, "banana": 2, "cherry": 1}

Note: For large tuples, collections.Counter is more efficient than repeated .count() calls.

Validation - checking for duplicates

Check if a tuple has any duplicate values:

items = (1, 2, 3, 4, 5, 3)
has_duplicate = any(items.count(item) > 1 for item in items)
print(has_duplicate)
# True

Performance Note

  • .count() performs a linear scan — O(n) time complexity
  • It traverses the entire tuple regardless of how many matches found
  • Since tuples are immutable and often used for fixed data, this is typically acceptable
  • For repeated counts on the same tuple, consider building a collections.Counter once:
from collections import Counter

data = ("a", "b", "a", "c", "a", "b")
counter = Counter(data)
print(counter["a"])  # 3 — O(1) lookup after O(n) build
print(counter["b"])  # 2

See Also