How To Find Index Of Element In Tuple Using Python

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.

Output

1
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 an only 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.

Output

index of given element 23 is: 1

The above example finds the index of the element 23 and prints the index in the output. The output shows that the index of the 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 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.

Output

Ram Index is: 0
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 perform the iteration and upon execution of each iteration, it prints the element of tuple with its index.

You may also like to read

Hope, you like this post of how to find the index of the element using Python. If you have any query regarding the tutorial, please comment below.

Reference
Programmiz Tutorial on Tuple Indexing

Also tell me, which method you are using to get the index of tuple element in Python.

Leave a comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.