Python List index() Function
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()
function returns the index of the given element in the list.
If the element is not found, a ValueError
exception is raised.
The index()
method only returns the first occurrence of the matching element.
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: 2
output
2
Example 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: 1
output
1
Example 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 list
output
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 list
To 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.
The returned index is computed relative to the beginning of the full sequence rather than the start
argument.
my_list = [0, 1, 2, 3, 4, 5, 4, 3, 2, 1, 0]
# index of 3
index = my_list.index(3)
print(index) # Output: 3
# index of 3 after 4th index
index_with_start = my_list.index(3, 4)
print(index_with_start) # Output: 7
# index of 3 between 4th and 6th index
index_with_start_end = my_list.index(3, 4, 6)
print(index_with_start_end) # Raises ValueError
output
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 list
Example 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]