Lesson 29 of 70 – Python Tuple Methods
41%

Python Tuple Methods

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.

Note: Tuples are immutable, so Python provides fewer methods for tuples compared to lists.
1. Tuple Methods in Python

Python provides two main built-in methods for tuples:

  • count() – Counts how many times a value appears.
  • index() – Returns the position of the first occurrence of a value.
numbers = (10, 20, 10, 30, 10)

print(numbers.count(10))
print(numbers.index(20))
Output:
3
1
2. count() Method

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))
Output:
3

Here, 10 appears three times in the tuple.

3. count() Syntax

The syntax of the count() method is:

tuple.count(value)

Example:

colors = ("red", "blue", "red", "green")

print(colors.count("red"))
Output:
2
4. Counting Strings

The count() method can also be used with strings stored inside a tuple.

fruits = ("apple", "banana", "apple", "mango", "apple")

print(fruits.count("apple"))
Output:
3
5. Counting Numbers
numbers = (5, 10, 5, 20, 5, 30)

print(numbers.count(5))
print(numbers.count(20))
print(numbers.count(100))
Output:
3
1
0

If the value does not exist, count() returns 0.

6. count() with No Matching Value
numbers = (10, 20, 30, 40)

print(numbers.count(50))
Output:
0

No error occurs when the value is not present.

7. index() Method

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))
Output:
2

Remember that Python indexing starts from 0.

8. index() Syntax

The basic syntax is:

tuple.index(value)

Example:

names = ("Amit", "Rahul", "Priya", "Neha")

print(names.index("Priya"))
Output:
2
9. index() with Duplicate Values

If a value occurs multiple times, index() returns the position of its first occurrence.

numbers = (10, 20, 10, 30, 10)

print(numbers.index(10))
Output:
0

Although 10 appears at indexes 0, 2 and 4, index() returns only the first position.

10. index() When Value Does Not Exist

If the specified value is not present in the tuple, index() raises a ValueError.

numbers = (10, 20, 30)

print(numbers.index(50))
Result:
ValueError: tuple.index(x): x not in tuple
11. index() with Start Position

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))
Output:
3

Python starts searching from index 2, so it finds 20 at index 3.

12. index() with Start and End

The index() method can also use both start and end positions.

numbers = (10, 20, 30, 20, 40, 50)

print(numbers.index(20, 2, 5))
Output:
3

The search is performed within the specified range.

13. count() and index() Together
numbers = (10, 20, 10, 30, 10)

print("Count:", numbers.count(10))
print("First Index:", numbers.index(10))
Output:
Count: 3
First Index: 0
14. Tuple Methods with Strings
languages = ("Python", "Java", "Python", "C++")

print(languages.count("Python"))
print(languages.index("Python"))
Output:
2
0
15. Tuple Methods with Mixed Data

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))
Output:
2
2
16. Difference Between count() and index()
Method Purpose Result
count() Counts occurrences Number
index() Finds first position Index number
17. Example: Student Marks
marks = (80, 75, 90, 80, 85)

print("80 appears:", marks.count(80), "times")
print("First 80 is at index:", marks.index(80))
Output:
80 appears: 2 times
First 80 is at index: 0
18. Example: Finding a Student
students = ("Amit", "Rahul", "Priya", "Neha")

name = "Priya"

print("Student found at index:", students.index(name))
Output:
Student found at index: 2
19. Important Point About index()

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")
Output:
Value not found

Using the in operator before index() can help avoid a ValueError.

20. Tuple Methods vs List Methods

Tuples are immutable, so they do not have methods such as append(), remove(), sort(), or reverse().

The main tuple-specific methods commonly used are:

  • count()
  • index()
21. Practical Example
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)
Output:
Mouse Count: 2
First Mouse Position: 1
22. Key Points
  • Tuples have two main built-in methods: count() and index().
  • count() returns the number of occurrences of a value.
  • index() returns the index of the first occurrence.
  • Python indexing starts from 0.
  • count() returns 0 when a value is not found.
  • index() raises a ValueError when a value is not found.
  • index() can use optional start and end positions.
  • Tuples are immutable, so their elements cannot be changed.

🧠 Quick Quiz

Question: Which tuple method counts how many times a value appears?