Python Tuple index()

The index() method searches an element in a tuple and returns its index.

In simple terms, index() method searches for the given element in a tuple and returns its position.

However, if the same element is present more than once, the first/smallest position is returned.

Note: Remember index in Python starts from 0 and not 1.


The syntax of index() method for Tuple is:

tuple.index(element)

Tuple index() parameters

index() method takes a single argument:

  • element - element that is to be searched.

Return value from Tuple index()

The index method returns the position/index of the given element in the tuple.

If no element is found, a ValueError exception is raised indicating the element is not found.


Example 1: Find position of element in the tuple

# vowels tuple
vowels = ('a', 'e', 'i', 'o', 'i', 'u')

# element 'e' is searched
index = vowels.index('e')

# index is printed
print('The index of e:', index)

# element 'i' is searched
index = vowels.index('i')

# only the first index of the element is printed
print('The index of i:', index)

When you run the program, the output will be:

The index of e: 1
The index of e: 2

Example 2: Index of element not present in the tuple

# vowels tuple
vowels = ('a', 'e', 'i', 'o', 'u')

# element 'p' is searched
index = vowels.index('p')

# index is printed
print('The index of p:', index)

When you run the program, the output will be:

ValueError: tuple.index(x): x not in tuple

Example 3: Find position of tuple and list inside the tuple

# random tuple
random = ('a', ('a', 'b'), [3, 4])

# element ('a', 'b') is searched
index = random.index(('a', 'b'))

# index is printed
print("The index of ('a', 'b'):", index)

# element [3, 4] is searched
index = random.index([3, 4])

# index is printed
print("The index of [3, 4]:", index)

When you run the program, the output will be:

The index of ('a', 'b'): 1
The index of [3, 4]: 2

This function is also used for following native datatypes: