index()
method in a python list returns the index at which an element is present in the list.
List elements in a python list have numeric indexes where the index of first element from left is 0, second element is 1 and so on.
index()
finds the position or index of first occurrence of the item. If an element occurs more than once in the list, then index()
will return the index at which the element is found first.
If the value is not present in the list, index()
raises a ValueError.
index() syntax
index()
method accepts the following arguments:
1. value: This is the element whose index we need to determine.
2. start index: Index from which it starts searching for the element. This argument is optional and defaults to 0. If not given, index()
will search from the beginning of list.
3. end index: Index till which index()
searches for the element. This argument is optional and defaults to 0. If not given, index() will search till the end of list.
Note that if only one argument is given, index()
will consider it as value to search.
If two arguments are given, then index()
will consider them as the value and start index while end index will default to the index of last list element.
Below is an example program to demonstrate the working of list
index()
method. # define a list numbers = [1, 4, 3, 2, 1, 5, 12, 4, 16, 6] # find index of 4 print('Index of 4 is:', numbers.index(4)) # find index of 3 print('Index of 3 is:', numbers.index(3))
This prints
Index of 4 is: 1
Index of 3 is: 2
Note that 4 occurs twice in the list: one at second position(index 1) and other at eighth position(index 7) but index()
method returns the position of its first occurrence.
Remember that elements in a list start at index 0.
index() with start and end arguments
Below is an example to demonstrate the usage of arguments in index()
method.
# define a list numbers = [1, 4, 3, 2, 1, 5, 12, 4, 16, 6] print('Index of 1 in complete list:',numbers.index(1)) # find from 3rd position print('Index of 1 starting from second position:', numbers.index(1,2)) # find between 3rd and 9th position print('Index of 4 between third and eighth position:', numbers.index(4,2,8))
which prints
Index of 1 in complete list: 0
Index of 1 starting from third position: 4
Index of 4 between third and eighth position: 7
Above example finds the index of elements
A. In the complete list
It searches for the index of 1 using
index()
with a single argument. Since 1 is present at the first position, index()
returns 0.B. With start position
index()
is supplied two arguments. First is the element to search and second is the starting index from where it should begin searching.Element to search is 1 and start index is 2. Since 1 is present at 5th position whose index will be 4, it returns 4. Notice that 1 is also present at index 0 but since we supplied 2 as the start index, it did not consider the first element.
C. Between a start and end position
Example searches for 4 between indexes 2 and 8 and returns 7 since 4 is found at this index.
Notice that 4 is also present at index 1 but it does not fall in between 2 and 8, the range we supplied to
index()
. ValueError
As stated earlier, if index()
could not find the value being searched in the list, it raises a ValueError as shown below.
# define a list numbers = [1, 4, 3, 2, 1, 5, 12, 4, 16, 6] # find index of non-existing element print(numbers.index(25))
which outputs
ValueError: 25 is not in list