![]() |
VOOZH | about |
index() method in Python is a helpful tool when you want to find the position of a specific item in a list. It works by searching through the list from the beginning and returning the index (position) of the first occurrence of the element you're looking for. Example:
1
Explanation: index("dog") method finds the first occurrence of "dog" in the list a. Since "dog" is at index 1, it returns 1.
list.index(element, start, end)
Parameters:
Returns:
Example 1: In this example, we are searching for the index of the number 40 within a specific range of the list from index 4 to 7 .
5
Example 2: In this example, we try to find the index of 'yellow' in the list and handle the error with a try-except block if it's not found.
Not Present
Example 3: In this example, we are finding the index of the tuple ("Bob", 22) in a list of tuples and index() will return the position of its first occurrence.