Post

Python Tuple index() Method

The index() method returns the index of the first occurrence of a specified value in a tuple.

Python Tuple index() Method

The index() method finds the position of a specific element in a tuple.

The syntax of the index() method is:

1
tuple.index(value[, start[, end]])

index() Parameters

The index() method takes up to three parameters:

  • value - the element to search for in the tuple
  • start (Optional) - search start position. Default is 0
  • end (Optional) - search end position. Default is the end of tuple

Example 1: How to use index() method in python?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
# create a tuple
numbers = (1, 2, 3, 2, 5, 2, 6)

# get the index of first occurrence of 2
index = numbers.index(2)
print(index)

# get the index of 2 starting from position 2
index = numbers.index(2, 2)
print(index)

# get the index of 2 between positions 2 and 5
index = numbers.index(2, 2, 5)
print(index)

Output:

1
2
3
1
3
3

Rules of index()

  • Raises ValueError if the value is not found
  • Returns the index of first occurrence only
  • Search range can be specified using start and end parameters
  • The end index is not included in the search
This post is licensed under CC BY 4.0 by the author.