In this tutorial, we’ll learn how to properly use Python tuple index() method with the help of multiple Python code examples.
Introduction: Python Tuple index() Method
This method is used to return the index value of an item in Python tuple.
Syntax of index() Method
tupleName.index(element, startRange(optional), endRange(optional))
- The index method takes three arguments.
- First one is the item of tuple which position(index) is to be searched.
- Second argument(startRange) is optional. By using it, we can specify from which item the search should start.
- Third argument(endRange) is also optional. We can use it to specify to which item the search should stop.
- Basically, these two arguments(startRange,endRange) is used to specify a range in which the search should take place inside Python tuple.
- Python tuple index() method returns an index value of the item passed in it. If the specified item is not found in Python tuple then a value error exception will occur.
Example 1: Get Index of an Item in Python Tuple
tupleValues=(2,5,6,'white',34.6,[3,6,2]) print( tupleValues.index(34.6) ) print( tupleValues.index([3,6,2]) ) print( tupleValues.index('white') ) print( tupleValues.index(2) )
Output
4 // index value of 34.6 5 // index value of [3,6,2] 3 // index value of string'white' 0 // index value of 2
As you can see in the above output that index value of each specified item is returned by Python tuple index() method.
Example 2: Duplicate items in Python Tuple
Let’s see which item’s index value will be returned if the specified item is used more than one time(duplicate items) in Python tuple. See below code:
tupleValues=(2,5,6,4.5,45,6,2,1,4) // 6 is used 2 times in tuple print( tupleValues.index(6) )
Output
2 // the first occurence of specified item is in 3rd position(0,1,2) so it returned 2
In case of duplicate items, the first occurrence of specified item’s index value will be returned.
Example 3: Specified Item not Found
tupleValues=(2,5,6) print( tupleValues.index(23) )
Output
ValueError: tuple.index(x): x not in tuple
Python tuple index() method throws a value error if the specified item is not found in tuple.
Example 4: Specifying Range in Python Tuple
We can specify a range inside tuple so the item will be searched only in that specific range. See below code:
tupleValues=(1,2,3,4,5,6,7,8,9,10,5,45) print( tupleValues.index(5,2,7) ) // 2(0,1,2) is starting range and 7(0,1,2,3,4,5,6,7) is ending range
Output
4 // the item is in index 4(0,1,2,3,4 means 5th position)
Let’s take another example in which the specified item will be present inside tuple but not inside the range specified. See below code:
tupleValues=(1,2,3,4,6,7,8,9,10,5,45) print( tupleValues.index(5,2,7) )
Output
ValueError: tuple.index(x): x not in tuple
Specifying a range will search this item only inside that specific range.
We can even specify only a starting range. See below code:
tupleValues=(1,2,3,4,6,7,8,9,10,5,45) print( tupleValues.index(2,4) ) // 4 is starting range in it(0,1,2,3,4)
Output
ValueError: tuple.index(x): x not in tuple
The item is present but it comes before the starting range,.
Example 5: index() Method Return Value
tupleValues=(1,2,3,4,6) returnedValue=tupleValues.index(2) print(returnedValue)
Output
1
As you can see here, the index() method returns the index value of item passed to it.
Conclusion
To conclude, now you have a detailed practical knowledge of how to easily use Python tuple index method. I’ll be very glad to have your feedback on this post. Thank you for reading it.