Table of Contents
The index() method returns the index of the specified element in the tuple.
The syntax of the tuple index() method is:
tuple.index(element, start, end)
1. Tuple index() parameters
The tuple index() method can take a maximum of three arguments:
- element – the element to be searched
- start (optional) – start searching from this index
- end (optional) – search the element up to this index
2. Return Value from Tuple index()
- The
index()method returns the index of the given element in the tuple. - If the element is not found, a
ValueErrorexception is raised.
Note: The index() method only returns the first occurrence of the matching element.
3. Example 1: Find the index of the element
# vowels tuple
vowels = ('a', 'e', 'i', 'o', 'i', 'u')
# index of 'e' in vowels
index = vowels.index('e')
print('The index of e:', index)
# element 'i' is searched
# index of the first 'i' is returned
index = vowels.index('i')
print('The index of i:', index)
Output
The index of e: 1 The index of e: 2
4. Example 2: Index of the Element not Present in the Tuple
# vowels tuple
vowels = ('a', 'e', 'i', 'o', 'u')
# index of'p' is vowels
index = vowels.index('p')
print('The index of p:', index)
Output
ValueError: tuple.index(x): x not in tuple
5. Example 3: Working of index() With Start and End Parameters
# alphabets tuple
alphabets = ('a', 'e', 'i', 'o', 'g', 'l', 'i', 'u')
# index of 'i' in alphabets
index = alphabets.index('e') # 2
print('The index of e:', index)
# 'i' after the 4th index is searched
index = alphabets.index('i', 4) # 6
print('The index of i:', index)
# 'i' between 3rd and 5th index is searched
index = alphabets.index('i', 3, 5) # Error!
print('The index of i:', index)
Output
The index of e: 1 The index of i: 6 Traceback (most recent call last): File "<string>", line 13, in <module> ValueError: tuple.index(x): x not in tuple
Related posts:
Python Program to Sort Words in Alphabetic Order
Python Deep Learning - Valentino Zocca & Gianmario Spacagna & Daniel Slater & Peter Roelants
Python del Statement
Python float()
Python compile()
Python 3 for Absolute Beginners - Tim Hall & J.P Stacey
Python Program to Check Leap Year
Python locals()
Python Program to Iterate Over Dictionaries Using for Loop
Python String startswith()
Python Program to Check If a String Is a Number (Float)
Python Program to Differentiate Between del, remove, and pop on a List
Python Set difference()
Python Program to Display the multiplication Table
Python List reverse()
Python Namespace and Scope
How to get current date and time in Python?
Python Program to Find the Factorial of a Number
Python Dictionary pop()
Python pow()
Python Sets
Python Inheritance
Python super()
Python Program to Find the Square Root
Python frozenset()
Python Set symmetric_difference_update()
Introduction to Machine Learning with Python - Andreas C.Muller & Sarah Guido
Python memoryview()
Building Machine Learning Systems with Python - Willi Richert & Luis Pedro Coelho
Python Program to Convert Decimal to Binary, Octal and Hexadecimal
Python Program to Check if a Key is Already Present in a Dictionary
Python Program to Add Two Matrices