Find the index of an item in List in Python

In this article, we will discuss different ways to find the index of a given item in a list in Python.

Table of Contents

Find index of an item in list using index() function

In Python, the List class provides a member function index(). It accepts an element as an argument and returns the index of first occurrence of that element in the list. It that element doesn’t exists in the list, then it raises the ValueError.

Let’s see an example,

listOfNums = [43, 45, 78, 56, 23, 34, 89, 12]

num = 78

# Get the index of number 78 in List
indexPos = listOfNums.index(num)

print('Index Pos : ', indexPos)

Output

Index Pos :  2

It returned the index of number 78 in the list. As indexing starts from 0 in Python, therefore the index of 3rd element of list is 2.

But if we try to look for an element that doesn’t exist in the list, then it will raise ValueError. Let’s see an example,

listOfNums = [43, 45, 78, 56, 23, 34, 89, 12]

num = 80

# Get the index of num in List
indexPos = listOfNums.index(num)

print('Index Position : ', indexPos)

Output:

ValueError: 80 is not in list

As number 80 doesn’t exist in the list, therefore the index() raised the ValueError. Now to handle this kind of scenario, we should first check if the given element exists in the list or not. If yes, then try to find its index position in the list. For example,

listOfNums = [43, 45, 78, 56, 23, 34, 89, 12]

num = 80

if num in listOfNums:
    # Get the index of num in List
    indexPos = listOfNums.index(num)
    print('Index Pos : ', indexPos)
else:
    print('Element does not exist in the list')

Output:

Element does not exist in the list

Apart from this you can also use the try except. For example,

listOfNums = [43, 45, 78, 56, 23, 34, 89, 12]

num = 80

try:
    # Get the index of num in List
    indexPos = listOfNums.index(num)
    print('Index Pos : ', indexPos)
except ValueError as e:
    print('Element does not exist in the list')

Output:

Element does not exist in the list

Find all indices of an item in list

List’s index() function returns the index position of first occurrence of given element. If you need to find the indices of all occurrences of a given element in list, then you need to keep calling the index() function in a loop till all occurrences are covered. For example,

listOfNums = [43, 45, 78, 56, 45, 34, 45, 12]

num = 45
indices = []
startPos = 0
found = True

while found:
    try:
        # Get the index of num in List from 
        # index startPos till the end of list 
        indexPos = listOfNums.index(num, startPos)
        # Add index position to the list
        indices.append(indexPos)
        startPos = indexPos + 1
    except ValueError as e:
        # If element does not exists 
        # in the range then exist the loop
        found = False

print('All Indices : ', indices)

Output:

All Indices :  [1, 4, 6]

It returned the index positions of all occurrences of number 45 in the list.

As soon as first occurrence is found, store its index position in a list and then again look for next occurrence of same element in the remaining part of list. For that we need to keep changing the starting point in the index() function. First time, starting position will be the 0th index position. After that, starting position will be one position after the last found index position.

Summary

We learned about different ways to find the index position of an element in the list in Python. Thanks.

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top