In this tutorial, learn how to find index of element in tuple with Python. The short answer is: use the Python index()
function to get the index of the given element.
You can find the index of the single or multiple elements from the Python tuple. If you have an element to find the index from all the tuple elements. You can use the for loop to determine the index of the given element from the tuple.
How to Find Index of Element in Tuple With Python
If you want to find the index of a single element in the tuple, you have to use the index()
function. The index function takes a single argument as the tuple element to pass.
String elements of the tuple can be pass as the argument within the double quotes(”). While integer elements of the tuple can be pass as the argument without the quotes(”). See the example below to get the index of your tuple element in Python.
1 2 3 |
myTuple = ("Ram", 23, 10, "Dara", 17, "Raju"); print(myTuple.index(23)); print(myTuple.index("Dara")); |
Output
3
The above example showing the index of the element “23” and “Dara”. The first element is an integer and the second element is a string.
You can easily find the index of any element you want. However, you will get the index of only a single element at a time. To find the index of more elements, you have to use the index() function again.
Find the Index of Given Tuple Element in Python
Suppose, you have a tuple element given in a variable. You can find the index of the given element by comparing it using the for loop. Use the if condition inside the for loop to compare the item with other elements. If the elements matching with the elements, the loop prints the element in the output with the index.
1 2 3 4 5 |
myTuple = ("Ram", 23, 10, "Dara", 17, "Raju"); myElement = 23; for t in myTuple: if t == myElement: print("index of given element", t, "is: ", myTuple.index(t)); |
Output
The above example finds the index of element 23 and prints the index in the output. The output shows that the index of element 23 is 1. This is again the method to find the index of the single element.
Get Index of All Tuple Elements with Python For Loop
In addition to the above all methods, you can find the index of all other elements of the tuple in Python. To perform this task, you have to just remove the if condition from the above example. Use the below-given method to print all the elements in the output with the relevant index.
1 2 3 |
myTuple = ("Ram", 23, 10, "Dara", 17, "Raju"); for t in myTuple: print(t, 'Index is:', myTuple.index(t)); |
Output
23 Index is: 1
10 Index is: 2
Dara Index is: 3
17 Index is: 4
Raju Index is: 5
The above example prints the index of all the elements with the single in the single line. The loop performs the iteration and upon execution of each iteration, it prints the element of tuple with its index.
DOWNLOAD Free PYTHON CHEAT SHEET
You may also like to read
- Find the index of the given element of the list Using Python
- Access string character by index using Python
- Unpack tuple pairs in the list with Python
I hope you like this post on how to find the index of the element using Python.
Reference
Programmiz Tutorial on Tuple Indexing