The Wayback Machine - https://web.archive.org/web/20241206184555/https://www.geeksforgeeks.org/python-list-index/
Open In App

Python List index() – Find Index of Item

Last Updated : 13 Oct, 2024
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

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

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:

Python
#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:

  • 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: 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.

Python
# 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 2: 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. 

Python
# 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.

Python
# 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.

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

Python
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:

Python List index() – FAQs

Why is list index out of range?

The “list index out of range” error occurs when you try to access an index that does not exist in the list. For example, if you have a list of length 5 and you try to access the element at index 10, you’ll get this error because the index is beyond the bounds of the list.

What does the index() method do in Python lists?

The index() method in Python lists returns the first occurrence of the specified value. It raises a ValueError if the value is not found in the list.

Can I specify a range to search within using the index() method?

Yes, you can specify a range to search within using the index() method by providing optional start and end parameters. The method will search for the value only within the specified range.

How do I use the index() method to find the index of a specific value in a list?

Here is how you can use the index() method to find the index of a specific value in a list:

my_list = ['a', 'b', 'c', 'd', 'b']
index_of_b = my_list.index('b') # Finds the first occurrence of 'b'
print(index_of_b) # Output: 1

# With start and end parameters
index_of_b_in_range = my_list.index('b', 2, 5) # Finds 'b' in the range index 2 to 4
print(index_of_b_in_range) # Output: 4

What happens if the index is out of range in Python?

If you try to access an index that is out of range in Python, it raises an IndexError with the message “list index out of range”. This happens because the index you’re trying to access does not exist within the list’s bounds.

Example:

my_list = [1, 2, 3]
print(my_list[5]) # Raises IndexError: list index out of range


Next Article

Similar Reads

three90RightbarBannerImg