Skip to content
Related Articles

Related Articles

Python List index()

View Discussion
Improve Article
Save Article
Like Article
  • Difficulty Level : Easy
  • Last Updated : 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. 

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 Python List index()

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

Example 1: Find the index of the element

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 items
list2 = ['cat', 'bat', 'mat', 'cat', 'pet']
 
# Will print the index of 'cat' 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 '3' 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

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 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: Index of the Element not Present in the 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

Example 6: 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 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


My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!