Python tuple index()

In this post, we will see about Python tuple’s index method.Python tuple’s index method is used to find index of element in the tuple

Python tuple count syntax

tuple1 is object of the tuple and element is the object which you want to get index.

Python tuple count example

Python tuple index method is used to find index of element.If element is not present in the tuple, then it will raise ValueError.
Let’s understand index method with the example.

πŸ’» Awesome Tech Resources:
  • Looking for βš’οΈ tech jobs? Go to our job portal.
  • Looking for tech events? Go to tech events πŸ—“οΈ Calendar.️

Output:

Index of USA in tupleOfCounties: 2

If element is not present in the tuple, then it will raise ValueError.

Output:

—————————————————————————
ValueError Traceback (most recent call last)
in ()
1 tupleOfCounties=(‘China’,’India’,’USA’,’Bhutan’)
—-> 2 print(“Index of USA in tupleOfCounties:”,tupleOfCounties.index(‘Nepal’))
3

ValueError: tuple.index(x): x not in tuple

If there are multiple occurrences of element in the list, then index method will return first occurrence.

Output:

Index of India in tupleOfCounties: 1

That’s all about Python tuple index method.

Related Posts

Leave a Reply

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

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.