The List index() method finds the position of the first occurrence of the given element in the list.
If specified item is not found, it raises ValueError exception.
Syntax
my_list.index(item, start, end)index() Parameters
Python List index() method parameters::
| Parameter | Condition | Description |
|---|---|---|
item |
Required | Any item (of type string, list, set, etc.) you want to search for |
start |
Optional | An index specifying where to start the search. Default value is 0. |
end |
Optional | An index specifying where to stop the search. Default value is the end of the list. |
index() Return Value
Python List index() method returns the index of the given element in the list.
Examples
Example 1: Find the index of an element
For example, find the index of Anna in a list:
my_list = ['Ryan', 'Tom', 'Anna', 'David']
index = my_list.index('Anna')
print(index) # Output: 2output
2Example 2: Find the index of an element when there are duplicates
If the list has many instances of the specified item, the index() method returns only the index of first instance.
For example, find first occurrence of character d:
my_list = ['a','d','c','d','b','c','d','e','f','d','f','a']
index = my_list.index('d')
print(index) # Output: 1output
1Example 3: index() on Item that does Not Exist
The index() method raises a ValueError if specified item is not found in the list.
my_list = ['Tom', 'Ryan', 'David']
print(my_list.index('Anna')) # Raises ValueError: 'Anna' is not in listoutput
Traceback (most recent call last):
File "main.py", line 2, in <module>
print(my_list.index('Anna')) # Raises ValueError: 'Anna' is not in list
ValueError: 'Anna' is not in listTo avoid such exception, you can check if item exists in a list, using in operator inside if statement.
my_list = ['Tom', 'Ryan', 'David']
if 'Anna' in my_list: # check if exists in my_list before index()
print(my_list.index('Anna'))Example 4: Limit index() Search to Subsequence with start and end parameters
If you want to search the list not starting from the beginning, you have to specify the start parameter. Similarly, you can also specify where to stop the search with end parameter.
my_list = [0, 1, 2, 3, 4, 5, 4, 3, 2, 1, 0]
index = my_list.index(3)
print(index) # Output: 3
index_with_start = my_list.index(3, 4)
print(index_with_start) # Output: 7
index_with_start_end = my_list.index(3, 4, 6)
print(index_with_start_end) # Raises ValueErroroutput
3
7
Traceback (most recent call last):
File "main.py", line 12, in <module>
index_with_start_end = my_list.index(3, 4, 6)
ValueError: 3 is not in listExample 5: Finding All Occurrences with index() and List Comprehension
You can use List Comprehension and enumerate built-in function to return a list of all indexes where the specified item occurs in the list or string.
def find_all_indexes(item, list_or_string):
return [n for n, item_in_list in enumerate(list_or_string) if item_in_list == item]
print(find_all_indexes('banana', ['apple', 'banana', 'cherry', 'banana', 'date']))output
[1, 3]