tuple.count() vs tuple.index()
tuple.count() and tuple.index() are both Python Tuple Methods APIs, and choosing between them is a common point of confusion. This page compares their syntax, parameters, return values, and typical use cases so you can pick the right one. tuple.count(): Returns the number of occurrences of x in the tuple. Its syntax is tuple.count(x), and it returns integer tuple.index(): Returns the index of the first occurrence of x. Raises ValueError if not found. Its syntax is tuple.index(x, start=0, end=len(tuple)), and it returns index integer Example of tuple.count(): t = (1, 2, 2, 3)
t.count(2) # 2
t.count(4) # 0. Example of tuple.index(): t = (1, 2, 3, 2)
t.index(2) # 1
t.index(2, 2) # 3. Read the full references for tuple.count() and tuple.index() to see parameters, edge cases, and example code side by side. Both belong to the Tuple Methods category in the Python Reference documentation. Try both in the Python playground to see the behavioral difference live.