There might arise a situation where the user might want to find out the position of a specific data element in the tuple. For example, we want to find out the index of a specific purchased food item from a tuple of various data elements. Elements in a tuple can be accessed in the same way that we do in lists and strings. As a result, we can easily access elements by indexing. For this, we use the Python tuple index() function that is a tuple subclass method that returns the index value of the specified element in the tuple.
Definition
- The Python tuple index() function returns the index value of the first occurrence of the specified element in the tuple.
- Python tuple index() method is a built-in tuple function that returns an integer value denoting the index position of the specified element in the tuple if found.
Tuple index() Syntax
The syntax followed by the tuple() method is as follows:
tuple.index(element, start, end)
Tuple index() Parameters
The Python tuple() method accepts a maximum of 3 parameters:
- element – the element to be searched in the input string
- start (Optional) – integer value from where the search will start
- end (Optional) – integer value till which the search will end
Return Value from Tuple index()
The Python tuple index() function returns the following:
- If the element is found inside the input tuple, it returns the index value position at which the first occurrence of the element was found. It returns an integer value.
- If the element was not found in the input tuple iterable, the function throws a ValueError exception.
Example 1: Find the index of the element
If the element is found multiple times in the input tuple, the index() function only returns the index value of the first occurrence of that element.
Note – The indexing in Python starts from 0 and not 1. To understand the basic operability of Python tuple index(), let us look at the subsequent program.
Example
# Python program to illustrate index()
my_tuple = (1, 2, 5, 7, 9, 2, 4, 9, 1, 3, 5)
# index of the first '2' is returned
index = my_tuple.index(2)
print('Index of 2:', index)
index = my_tuple.index(9)
print('Index of 9:', index)
# index of '4' is returned
index = my_tuple.index(4)
print('Index of 4:', index)
print('')
my_tuple = ('Hi', 'Hello', 'Good', 'Hi', 'Morning', 'Welcome')
index = my_tuple.index('Hi')
print('Index of Hi:', index)
index = my_tuple.index('Morning')
print('Index of Morning:', index)
Output
Index of 2: 1
Index of 9: 4
Index of 4: 6
Index of Hi: 0
Index of Morning: 4
Example 2: Index of the Element not Present in the Tuple
Example
# Python program to illustrate index()
my_tuple = ('Hi', 'Hello', 'Good', 'Hi', 'Morning')
index = my_tuple.index('Welcome')
print('Index of Welcome:', index)
Output
Traceback (most recent call last):
File "", line 4, in
ValueError: tuple.index(x): x not in tuple
Example 3: Working of index() With Start and End Parameters
Example
# Python program to illustrate index()
alpha = ('a', 'f', 'r', 'w' ,'a', 'r', 's', 'a', 'f', 'r', 'a', 's')
index = alpha.index('a', 5)
print('Index of a:', index)
index = alpha.index('r', 1, 6)
print('Index of r:', index)
index = alpha.index('s', 8, 12)
print('Index of s:', index)
Output
Index of a: 7
Index of r: 2
Index of s: 11
Frequently Asked Questions
Q1. Can you index a tuple in Python?
There might arise a situation where the user might want to find out the position of a specific data element in the tuple iterable. For this issue, the Python tuple index() method is used.
Python tuple index() built-in function is used to return the index value of the first occurrence of an element from the input tuple if the element is found. If the element specified is not found, then it raises an exception.
The syntax for the Python tuple index() string function is as follows:
tuple.index(substr, start, end)
Q2. What is index in tuple?
Tuples are essentially data types in Python. These tuples are an ordered collection of different data types’ elements. Elements in a tuple can be accessed in the same way that we do in lists and strings. As a result, we can easily access elements by indexing. Furthermore, indexing is as straightforward as in lists, beginning at index zero. Indexing is the term used to describe this process. Indexing allows you to easily access individual elements/characters in a tuple using a numeric value.
Example
my_tuple = ('Car', 'Bike', 'Bus', 'TV', 'Laptop', 'Watch')
print('Element at index 2 is:', my_tuple[2])
print('Element at index 5 is:', my_tuple[5])
print('Element at index 0 is:', my_tuple[0])
print('Elements between index 2 and 5 is:', my_tuple[2:5])
Output
Element at index 2 is: Bus
Element at index 5 is: Watch
Element at index 0 is: Car
Elements between index 2 and 5 is: ('Bus', 'TV', 'Laptop')
Q3. How do you find the index of a tuple element in Python?
To find the index position of a specific element from the input tuple, we make use of the Python tuple index() method. The syntax for the Python index tuple function is as follows:
tuple.index(substr, start, end)
Example
my_tuple = ('Python', 'Java', 'C', 'C++', 'SQL', 'Ruby', 'Perl', 'MATLAB')
print("Index of 'Java':", my_tuple.index('Java'))
print("Index of 'Ruby':", my_tuple.index('Ruby'))
print("Index of 'C':", my_tuple.index('C', 3, 6))
Output
Index of 'Java': 1
Index of 'Ruby': 5
Traceback (most recent call last):
File "", line 5, in
ValueError: tuple.index(x): x not in tuple
Leave a Reply