Python List index()

21 Jun, 2022
Python index() is an inbuilt function in Python, which searches for a given element from the start of the list and returns the lowest index where the element appears. 
In this article, we will cover different examples to find the index, such as:
Find the index of the element
Working of the index() With Start and End Parameters
Working of the index() With two Parameters only
Index of the Element not Present in the List
How to fix list index out of range
Syntax: list_name.index(element, start, end)
Parameters:  
element – The element whose lowest index will be returned.
start (Optional) – The position from where the search begins.
end (Optional) – The position from where the search ends.
Returns: the lowest index where the element appears.
Error: If any element which is not present is searched, it returns a ValueError
In this example, we will try to find out the index of an element of “bat” from python to get the index of items in the list.
Python3
# list of itemslist2 = ['cat', 'bat', 'mat', 'cat', 'pet'] # Will print the index of 'cat' in list2print(list2.index('bat'))
Output:  
1
 In this example, we find an element in list python, the index of an element of 4 in between the index at the 4th position and ending with the 8th position. 
Python3
# list of itemslist1 = [1, 2, 3, 4, 1, 1, 1, 4, 5] # Will print index of '4' in sublist# having index from 4 to 8.print(list1.index(4, 4, 8))
Output: 
7
In this example, we will see when we pass two arguments in the index function, the first argument is treated as the element to be searched and the second argument is the index from where the searching begins. 
list_name.index(element, start)
Python3
# list of itemslist1 = [6, 8, 5, 6, 1, 2] # Will print index of '3' in sublist# having index from 1 to end of the list.print(list1.index(6, 1))
Output:
3
In this example, we will see that if the Index of the Element will not Present in the List it will through a ValueError.
Python3
# Python3 program for demonstration# of index() method error list1 = [1, 2, 3, 4, 1, 1, 1, 4, 5] # Return ValueErrorprint(list1.index(10))
Output: 
Traceback (most recent call last):
  File "/home/b910d8dcbc0f4f4b61499668654450d2.py", line 8, in 
    print(list1.index(10))
ValueError: 10 is not in list
In this example, The third argument which is the end, itself is not included in the range from start to end, i.e the searching takes place from start to end-1 index. Hence, The end index passed as an argument is not included.
list_name.index(element, start, end)
Python3
# Python3 program for demonstration# of index() method list1 = [6 , 2 , 14 , 8 , 9 , 10] # return error as index '4' is not included in the range# having index from 1 to 4.print(list1.index(9, 1, 4))
Output:
Traceback (most recent call last):
  File "/home/3cbe5b7d0595ab3f8564f16af7a15172.py", line 9, in <module>
    print(list1.index(9 , 1 , 4))
ValueError: 9 is not in list
Here we are going to create a list and then try to iterate the list using the constant values in for loops.
Python3
li = [1,2 ,3, 4, 5] for i in range(6):    print(li[i])
Output:
1
2
3
4
5
IndexError: list index out of range
Reason of the error –  The length of the list is 5 and if we are an iterating list on 6 then it will generate the error.
Solving this error without using len() or constant Value:
To solve this error we will take the index of the last value of the list and then add one then it will become the exact value of length.
Python3
li = [1,2 ,3, 4, 5] for i in range(li.index(li[-1])+1):    print(li[i])
Output:
1
2
3
4
5
riyaaggarwaldtu
AmiyaRanjanRout
susobhanakhuli
surajkumarguptaintern
Python-Built-in-functions
python-list
python-list-functions
Python
python-list
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Different ways to create Pandas Dataframe
Python String | replace()
How to Install PIP on Windows ?
*args and **kwargs in Python
Python Classes and Objects
Python OOPs Concepts
Convert integer to string in Python
Introduction To PYTHON
Create a Pandas DataFrame from Lists
How to drop one or multiple columns in Pandas Dataframe