Python Tuple index() Method
tuple.index() finds the position of a value inside a tuple.
It is useful when you know the value you want to search for and need its index. This method returns the first matching position and raises an error if the value is not found.
Quick example
colors = ("red", "blue", "green", "blue")
position = colors.index("blue")
print(position) # 1
tuple.index(value) returns the position of the first matching item.
What tuple.index() does
The index() method:
- Finds the position of a value inside a tuple
- Returns the index of the first match
- Works only if the value exists in the tuple
- Uses zero-based indexing, so the first item is at index
0
Example:
numbers = (10, 20, 30, 20)
print(numbers.index(20))
Output:
1
Even though 20 appears twice, index() returns only the first match.
If you are new to tuples, see what a tuple is in Python.
Basic syntax
You can use index() in two ways:
tuple_name.index(value)
or:
tuple_name.index(value, start, end)
Parameters
value— the item to search forstart— optional starting positionend— optional ending positionendis not included in the search
Example:
letters = ("a", "b", "c", "b", "d")
print(letters.index("b")) # 1
print(letters.index("b", 2)) # 3
print(letters.index("b", 1, 3)) # 1
What the method returns
tuple.index() returns:
- An integer
- The position of the first matching item
If the value appears more than once, later matches are ignored.
Example:
animals = ("cat", "dog", "bird", "dog")
result = animals.index("dog")
print(result)
Output:
1
When to use it
Use tuple.index() when:
- You need the position of a known value
- You want to find where an item first appears
- You are working with a small fixed collection stored as a tuple
If you already know the position, use normal tuple indexing instead, such as my_tuple[0]. For a full explanation, see Python tuple indexing explained.
What happens if the value is missing
If the value is not in the tuple, Python raises a ValueError.
Example:
colors = ("red", "blue", "green")
print(colors.index("yellow"))
This causes an error because "yellow" is not in the tuple.
A safer approach is to check first:
colors = ("red", "blue", "green")
if "yellow" in colors:
print(colors.index("yellow"))
else:
print("Value not found")
Output:
Value not found
If you want to learn more about this kind of error, see ValueError in Python: causes and fixes.
Using start and end
The start and end arguments let you search only part of a tuple.
This is helpful when the same value appears multiple times.
Example:
values = (5, 10, 5, 20, 5)
print(values.index(5)) # 0
print(values.index(5, 1)) # 2
print(values.index(5, 3, 5)) # 4
How it works
starttells Python where to begin searchingendtells Python where to stop searching- The
endposition itself is not included
Example:
values = (5, 10, 5, 20, 5)
print(values.index(5, 1, 4))
Output:
2
Python searches positions 1, 2, and 3, but not 4.
Beginner tips
Keep these points in mind when using tuple.index():
- Do not confuse the value with the index
- Remember that indexing starts at
0 - Use Python tuple
count()method if you need to know how many times a value appears - Use normal indexing like
my_tuple[0]when you already know the position
Example of value vs index:
fruits = ("apple", "banana", "orange")
print(fruits.index("banana")) # 1
print(fruits[1]) # banana
Here:
"banana"is the value1is the index
Common mistakes
These are common problems beginners run into with tuple.index():
- Searching for a value that is not inside the tuple
- Expecting
index()to return all matching positions - Forgetting that the first item is at index
0 - Using the wrong search range with
startandend - Confusing
tuple.index()with similar methods on strings or lists
Useful debugging checks
These quick checks can help:
print(my_tuple)
print(len(my_tuple))
print(value in my_tuple)
print(my_tuple.count(value))
print(my_tuple.index(value))
Be careful with the last line. It will raise a ValueError if value is missing.
A safer version is:
if value in my_tuple:
print(my_tuple.index(value))
else:
print("Value not found")
FAQ
What does tuple.index() return?
It returns the index of the first matching value in the tuple.
What if the value appears more than once?
tuple.index() returns only the first match.
What happens if the value is not in the tuple?
Python raises a ValueError.
Can I search only part of a tuple?
Yes. You can use start and end arguments to limit the search.
Is tuple.index() the same as list.index()?
They work in a very similar way, but one is for tuples and the other is for lists.