Python List index()
Python index() is an inbuilt function in Python, which searches for a given element from the start of the list and returns the index of the first occurrence.
How to find the index of an element or items in a list
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 of index() Method
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.
Return: Returns the lowest index where the element appears.
Error: If any element which is not present is searched, it raises a ValueError.
Example 1: Find the index of the element
Finding index of ‘bat’ using index() on Python List list2
Python3
# list of items list2 = [ 'cat' , 'bat' , 'mat' , 'cat' , 'pet' ] # Will print the index of 'bat' in list2 print (list2.index( 'bat' )) |
Output:
1
Example 2: Working of the index() With Start and End Parameters
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 items list1 = [ 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
Example 3: Working of the index() With two Parameters only
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 items list1 = [ 6 , 8 , 5 , 6 , 1 , 2 ] # Will print index of '6' in sublist # having index from 1 to end of the list. print (list1.index( 6 , 1 )) |
Output:
3
Example 4: Index of the Element not Present in the List
Python List index() raises ValueError, when the search element is not present inside the List.
Python3
# Python3 program for demonstration # of index() method error list1 = [ 1 , 2 , 3 , 4 , 1 , 1 , 1 , 4 , 5 ] # Return ValueError print (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
Example 5: How to fix list index out of range using Index()
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 for 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]) |
1 2 3 4 5
Please Login to comment...