89DEVs

Python Tuple index() Method

tuple.index() overview

The Tuple index() method returns the index of a given element. It is useful to find elements in a Tuple to select them later. Use this method to prevent IndexError exceptions, which are raised when an index is out of range. It is helpful to check first if the element exists to prevent ValueError exceptions.

tuple.index() example

How to find the index of a given element? To find the index of a given element in a tuple we call tuple.index(). # Tuple index() example letters = ('a', 'b', 'c', 'd', 'e') index_of_a = letters.index('a') print('the index of a in the tuple is', index_of_a) The index of the element is returned and we can use it for further operations. the index of letter a in the tuple is 0

prevent ValueError exceptions

What happens when we use the index() method on an element that is not in the Tuple? # find index for element not in tuple letters = ('a', 'b', 'c', 'd', 'e') index_of_z = letters.index('z') If the element is not in the Tuple a ValueError exception is raised. ValueError: tuple.index(x): x not in tuple To prevent this, we can check first if the element is in Tuple with the in operator. # check if element is in tuple letters = ('a', 'b', 'c', 'd', 'e') 'z' in letters The letter z is not in our Tuple, so False is returned. False To prevent ValueError exceptions we can check first if the element is in the Tuple and only use index() when True is returned. # check first if value is in tuple before using index() letters = ('a', 'b', 'c', 'd', 'e') if 'a' in letters: print(letters.index('a')) else: print('a is not in the tuple') if 'z' in letters: print(letters.index('z')) else: print('z is not in the tuple') The index for a is returned, but z is not in the tuple therefore the else statement is executed. 0 z is not in the tuple

tuple.index() for multiple occurrences

If the given element appears more than once, only the first index is returned. # Tuple index() for multiple occurrences letters = ('a', 'b', 'b', 'c', 'd', 'e') print(letters.index('b')) The element b appears at index 1 and 2. The first occurrence is returned. 1

tuple.index() with start and end arguments

The start parameter allows to begin the search at the specified index. And the end parameter allows to the search at the given index. # tuple index() method with start and end parameter letters = ('a', 'b', 'b', 'c', 'd', 'e') # begin search at 2nd element and end at 4th element print(letters.index('b', 2, 4)) The index of the first b element is 1, but we specified to begin the search at the 2nd element. Therefore index 2 is returned. 2

tuple.index() syntax

The syntax of the Tuple index() method is: tuple.index(element, [start], [end])

tuple.index() arguments

The Tuple index() method accepts between 1 and 3 arguments:
  • required: element, the given element to find the index for
  • optional: start, the index to begin the search
  • optional: end, the index to end the search

tuple.index() return value

The Tuple index() method returns the index of the given element as an integer. If the element occurs multiple times, the index of the first occurrence is returned. If the element is not found, a ValueError exception is raised. To prevent this use the in operator.

Related Tuple methods

Tuples have 2 methods in Python. The other Tuple method is tuple.count() and returns the count for a given element.

                
        

Summary


Click to jump to section