Python Program to find Index of a Tuple Item

Write Python Program to find the Index of a given Tuple Item within a tuple. Python has a tuple index function that returns the index of a given tuple item. The tuple index function returns the index position of the first found value. The syntax of the Python tuple index

TupleName.index(tupleValue, start, end)

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

# Index of a Tuple Item

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 Tuple index function output

Python Program to Find Index of a Tuple Item 1

Python Program to find Index of a Tuple Item Example 2

The Python tuple index code in the third example starts looking for ‘a’ from eight position and stops at the thirteenth position.

# Index of a Tuple Item

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)

Python Tuple Item index value output

('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 index 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 exits from the loop and print the index position of the first found tuple item. The second and third will print all the index positions of the tuple item.

# Index of a 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)

Tuple Item index value 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