Python Program to find Index of a Tuple Item

Write Python Program to find the Index of a given Item within a tuple. It has a built-in function that returns the index of a given tuple item. This function returns the index position of the first found value, and the syntax is

TupleName.index(tupleValue, start, end)

If you specify the start value, the function starts looking from that position. Similarly, if we set the end position, the tuple index function will stop looking at that number. The third example starts looking from the fourth position, and the next one will start looking from the seventh position.

numtup = (11, 33, 55, 77, 99, 111, 77, 121, 13, 55, 77)
print(numtup)

index1 = numtup.index(33)
print("Index Position of 33 = ", index1)

index2 = numtup.index(77)
print("Index Position of 77 = ", index2)

index3 = numtup.index(77, 4)
print("Index Position of 77 = ", index3)

index4 = numtup.index(77, 7)
print("Index Position of 77 = ", index4)
Python Program to Find Index of a Tuple Item 1

Python Program to find Index of a Tuple Item Example

The code in the third example starts looking for ‘a’ from the eighth position and stops at the thirteenth position.

strTup = tuple("tutorialgateway")
print(strTup)

index1 = strTup.index("a")
print("Index Position of a = ", index1)

index2 = strTup.index("t", 3)
print("Index Position of t = ", index2)

index3 = strTup.index("a", 8, 13)
print("Index Position of a = ", index3)

index4 = strTup.index("x")
print("Index Position of x = ", index4)
('t', 'u', 't', 'o', 'r', 'i', 'a', 'l', 'g', 'a', 't', 'e', 'w', 'a', 'y')
Index Position of a =  6
Index Position of t =  10
Index Position of a =  9
Traceback (most recent call last):
  File "/Users/suresh/Desktop/simple.py", line 13, in <module>
    index4 = strTup.index("x")
ValueError: tuple.index(x): x not in tuple

Python Program to find Index of a Tuple Item without using function.

In this example, we used for loop and enumerate to iterate tuple values. The lf statement in all the examples will check the index position of a given value in a tuple. The break statement in the first one will exit from the loop and print the position of the first found item. The second and third will print all the index positions of the tuple item.

numtup = (10, 20, 30, 20, 40, 50, 20, 70, 80, 20)
print(numtup)

val = 20
for i in range(len(numtup)):
    if numtup[i] == val:
        print("Index Position of 20 = ", i)
        break

print("All the Index Positions")
for i in range(len(numtup)):
    if numtup[i] == val:
        print("Index Position of 20 = ", i)

print("All the Index Positions using enumerate")
for i, x in enumerate(numtup):
    if x == val:
        print("Index Position of 20 = ", i)

Output

(10, 20, 30, 20, 40, 50, 20, 70, 80, 20)
Index Position of 20 =  1
All the Index Positions
Index Position of 20 =  1
Index Position of 20 =  3
Index Position of 20 =  6
Index Position of 20 =  9
All the Index Positions using enumerate
Index Position of 20 =  1
Index Position of 20 =  3
Index Position of 20 =  6
Index Position of 20 =  9