Index() Method

The Index() method returns the first occurrence of the given element from the tuple.

Syntax:

tuple.index(element, start, end)

Parameters:

  • element: The element to be searched.
  • start (Optional): The starting index from where the searching is started
  • end (Optional): The ending index till where the searching is done

Note: This method raises a ValueError if the element is not found in the tuple.

Example 1: Using Tuple Index() Method

Python3




# Creating tuples
Tuple = (0, 1, 2, 3, 2, 3, 1, 3, 2)
  
# getting the index of 3
res = Tuple.index(3)
print('First occurrence of 3 is', res)
  
# getting the index of 3 after 4th
# index
res = Tuple.index(3, 4)
print('First occurrence of 3 after 4th index is:', res)


Output:

First occurrence of 3 is 3
First occurrence of 3 after 4th index is: 5

Example 2: Using Tuple() method when the element is not found

Python3




# Creating tuples
Tuple = (0, 1, 2, 3, 2, 3, 1, 3, 2)
  
# getting the index of 3
res = Tuple.index(4)


Output:

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

Note: For more information on Python Tuples refer to Python Tuple Tutorial.



Python Tuple Methods

Python Tuples is an immutable collection of that are more like lists. Python Provides a couple of methods to work with tuples. In this article, we will discuss these two methods in detail with the help of some examples.

Similar Reads

Count() Method

The count() method of Tuple returns the number of times the given element appears in the tuple....

Index() Method

...

Contact Us