tuple.index()

tuple.index(x[, start[, end]])
Returns: int · Updated March 15, 2026 · Tuple Methods
tuples methods searching immutable

The .index() method returns the index of the first occurrence of a specified value in a tuple. If the value is not found, it raises a ValueError. You can optionally restrict the search to a slice of the tuple by providing start and end indices. This method is the primary way to find the position of an element in Python tuples.

Since tuples are immutable, .index() does not modify the original tuple — it only reads and searches. The method always returns the index of the first matching element, scanning from the beginning (or from start if specified).

Syntax

tuple.index(x[, start[, end]])

Parameters

ParameterTypeDefaultDescription
xanyThe value to search for. Can be of any type: int, str, float, list, dict, object, etc. Comparisons use ==.
startint0Optional start index. Search begins from this position. Defaults to 0.
endintlen(tuple)Optional end index. Search stops before this position. Defaults to the tuple length.

Return Value

Returns an int representing the index of the first occurrence of x. The index is 0-based, meaning the first element has index 0. If x is not found in the searched range, raises ValueError. The original tuple remains unchanged.

Examples

Basic usage - finding an element’s position

Find the index of a value in a tuple:

fruits = ("apple", "banana", "cherry", "banana")
index = fruits.index("banana")
print(index)
# 1

The method returns the first match, not all matches.

Finding the position of a number

numbers = (10, 20, 30, 40, 50)
print(numbers.index(30))
# 2
print(numbers.index(10))
# 0

Using the start parameter

Search from a specific position onward:

colors = ("red", "blue", "green", "blue", "yellow")
print(colors.index("blue", 2))  # Start searching from index 2
# 3

Using start and end parameters

Search within a specific slice:

letters = ("a", "b", "c", "d", "b", "e", "b")
print(letters.index("b", 1, 4))  # Search in indices 1-3
# ValueError: 'b' is not in list

print(letters.index("b", 1, 5))  # Search in indices 1-4
# 4

Handling missing values with try/except

Since .index() raises ValueError when not found, wrap it in try/except:

items = ("apple", "banana", "cherry")
try:
    idx = items.index("orange")
except ValueError:
    idx = -1
print(idx)
# -1

Finding index in a tuple of lists

matrix = ([1, 2], [3, 4], [5, 6])
print(matrix.index([3, 4]))
# 1

Demonstrating immutability

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

tup = ("a", "b", "c", "b", "d")
result = tup.index("b")
print(result)
# 1
print(tup)
# ('a', 'b', 'c', 'b', 'd')  # Original unchanged

Common Patterns

Checking if value exists before getting index

Avoid ValueError by checking membership first:

data = ("foo", "bar", "baz")
if "bar" in data:
    print(data.index("bar"))
# 1

This pattern is safer but traverses the tuple twice.

Finding all occurrences

Since .index() only returns the first match, use a list comprehension with enumerate for all positions:

items = ("a", "b", "a", "c", "a")
indices = [i for i, x in enumerate(items) if x == "a"]
print(indices)
# [0, 2, 4]

Using enumerate as an alternative

For more complex search conditions:

numbers = (1, 4, 2, 7, 5, 3)
first_odd = next((i for i, n in enumerate(numbers) if n % 2 == 1), -1)
print(first_odd)
# 1

Performance Note

  • .index() performs a linear scan — O(n) time complexity
  • Best case is O(1) when element is at the start
  • Worst case is O(n) when element is at the end or not present
  • The optional start and end parameters can reduce the search space, improving performance when you know roughly where the element should be

See Also