Python tuples have only two built-in methods: count() and index(). These methods are used to count values and find the position of a value inside a tuple.
Python provides two main built-in methods for tuples:
numbers = (10, 20, 10, 30, 10)
print(numbers.count(10))
print(numbers.index(20))
The count() method returns the number of times a specified value appears in a tuple.
numbers = (10, 20, 10, 30, 10)
print(numbers.count(10))
Here, 10 appears three times in the tuple.
The syntax of the count() method is:
tuple.count(value)
Example:
colors = ("red", "blue", "red", "green")
print(colors.count("red"))
The count() method can also be used with strings stored inside a tuple.
fruits = ("apple", "banana", "apple", "mango", "apple")
print(fruits.count("apple"))
numbers = (5, 10, 5, 20, 5, 30)
print(numbers.count(5))
print(numbers.count(20))
print(numbers.count(100))
If the value does not exist, count() returns 0.
numbers = (10, 20, 30, 40)
print(numbers.count(50))
No error occurs when the value is not present.
The index() method returns the position of the first occurrence of a specified value in a tuple.
numbers = (10, 20, 30, 40)
print(numbers.index(30))
Remember that Python indexing starts from 0.
The basic syntax is:
tuple.index(value)
Example:
names = ("Amit", "Rahul", "Priya", "Neha")
print(names.index("Priya"))
If a value occurs multiple times, index() returns the position of its first occurrence.
numbers = (10, 20, 10, 30, 10)
print(numbers.index(10))
Although 10 appears at indexes 0, 2 and 4, index() returns only the first position.
If the specified value is not present in the tuple, index() raises a ValueError.
numbers = (10, 20, 30)
print(numbers.index(50))
The index() method can accept a second argument called start. It tells Python where to begin searching.
numbers = (10, 20, 30, 20, 40)
print(numbers.index(20, 2))
Python starts searching from index 2, so it finds 20 at index 3.
The index() method can also use both start and end positions.
numbers = (10, 20, 30, 20, 40, 50)
print(numbers.index(20, 2, 5))
The search is performed within the specified range.
numbers = (10, 20, 10, 30, 10)
print("Count:", numbers.count(10))
print("First Index:", numbers.index(10))
languages = ("Python", "Java", "Python", "C++")
print(languages.count("Python"))
print(languages.index("Python"))
Tuples can contain different types of values, and these methods can search for individual values.
data = (10, "Python", 20.5, "Python", True)
print(data.count("Python"))
print(data.index(20.5))
| Method | Purpose | Result |
|---|---|---|
| count() | Counts occurrences | Number |
| index() | Finds first position | Index number |
marks = (80, 75, 90, 80, 85)
print("80 appears:", marks.count(80), "times")
print("First 80 is at index:", marks.index(80))
students = ("Amit", "Rahul", "Priya", "Neha")
name = "Priya"
print("Student found at index:", students.index(name))
Always remember that index() raises an error when the value does not exist.
colors = ("red", "blue", "green")
if "yellow" in colors:
print(colors.index("yellow"))
else:
print("Value not found")
Using the in operator before index() can help avoid a ValueError.
Tuples are immutable, so they do not have methods such as append(), remove(), sort(), or reverse().
The main tuple-specific methods commonly used are:
products = ("Laptop", "Mouse", "Keyboard", "Mouse", "Monitor")
mouse_count = products.count("Mouse")
mouse_position = products.index("Mouse")
print("Mouse Count:", mouse_count)
print("First Mouse Position:", mouse_position)
Question: Which tuple method counts how many times a value appears?