Open In App

Python List index()

Improve
Improve
Like Article
Like
Save
Share
Report

List index() method searches for a given element from the start of the list and returns the position of the first occurrence.

Example:

Python




# list of animals
Animals= ["cat", "dog", "tiger"]
# searching positiion of dog
print(Animals.index("dog"))


Output

1


Definition of Python List index()

Python list index() method is used to find position of element in list Python.

It returns the position of the first occurrence of that element in the list. If the item is not found in the list, index() function raises a “ValueError” error.

List 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.

How to Find Index of an Element in a List?

Using index() method in Python, you can find position of the first occurrence of an element in list.

Example:

Python3




#List of fruits
fruits = ["apple", "banana","cherry","apple"]
#Searching index of apple
print(fruits.index("apple"))


Output

0


More Example on List index() Method

We will cover different examples to find the index of element in list using Python, and explore different scenarios while using list index() method, such as:

  • Find the index of the element
  • Working on 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

Example 1: Find the index of the element

Finding an 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 on 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. 

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():

To solve this error we will take the count of the total number of elements inside the list and run a loop after that in the range of that count.

Python3




li = [1, 5, 3, 2, 4]
count=0
  
for num in li:
  count+=1
    
for i in range(count):
    print(li[i])


Output

1
5
3
2
4


Python list index() method is very useful when searching for an element in a list. Python list index() function works best in a list where every element is unique.

Hope you learnt about how to use index() function in Python? after reading this article.

Also Read:



Last Updated : 01 Dec, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads