Find the index of an item in List in Python

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

Table Of Contents

Find index of item in List using the index() function

In Python, list class has a function index(), which accepts a value and looks for value in the list. If value exists in the list, then it returns the index position of first occurrence of given value in the list, otherwise it will raise ValueError.

Let’s see an example. Here we have a list of integers and we will look for the index position of number 48 in the list.

listOfNums = [45, 45, 47, 48, 49, 50]

num = 48

# Find the index of value in the list
indexPos = listOfNums.index(num)

print('Index Position: ', indexPos)

Output

Advertisements
Index Position:  3

The index() function returned the index position of 48 in the list. But if we try to find an item that does not exist in the list, then index() function will raise ValueError. For example,

listOfNums = [45, 45, 47, 48, 49, 50]

num = 11

# Find the index of value in the list
indexPos = listOfNums.index(num)

print('Index Position: ', indexPos)

Output

Traceback (most recent call last):
  File "temp.py", line 6, in <module>
    indexPos = listOfNums.index(num)
ValueError: 11 is not in list

As number 11 does not exist in the list, therefore it raised the ValueError. So, to handle such kind of situation, we need to use try/except while looking for the element in list. For example,

listOfNums = [45, 45, 47, 48, 49, 50]

num = 11

try:
    # Find the index of value in the list
    indexPos = listOfNums.index(num)
    print('Index Position: ', indexPos)
except ValueError as e:
    print(e)

Output

11 is not in list

Find all indices of an item in list

In the previous example, we learned how to find the index of first occurrence of a value in a list. We can use the same index() function to find the indices of all occurrences of an item in a list. For that, we need to run a loop and keep looking for the given item in the list, till all occurrence of the given item is covered. We can use the other optional arguments of index() function i.e. the start and end arguments. If provided then index() function will look for the given value in that range only. So, if we can start from index position 0, then we will find the first occurrence of given item at index position n, then we can again look for the value from index position n+1. Till we are getting the new index positions, we will keep looking for the given value and as soon as the index() function returns -1, we will stop the iteration. All the indices, where given item is found, will be stored in a list.

For example,

listOfNums = [45, 48, 47, 48, 49, 48, 67, 48, 78, 48]

num = 48
startPosition = 0
indices = []
found = True

while found:
    try:
        # Find the index of value in the list
        indexPos = listOfNums.index(num, startPosition)
        indices.append(indexPos)
        startPosition = indexPos + 1
    except ValueError as e:
        found = False

print('Index Positions: ', indices)

Output:

Index Positions:  [1, 3, 5, 7, 9]

We collected all the index positions of item 48 in the list.

Summary

Today, we learned about different ways to find the index position of an element in a list in Python. We also discussed the way to find the index positions of all occurrences of an item in the list. Happy Coding.

Pandas Tutorials -Learn Data Analysis with Python

   

Are you looking to make a career in Data Science with Python?

Data Science is the future, and the future is here now. Data Scientists are now the most sought-after professionals today. To become a good Data Scientist or to make a career switch in Data Science one must possess the right skill set. We have curated a list of Best Professional Certificate in Data Science with Python. These courses will teach you the programming tools for Data Science like Pandas, NumPy, Matplotlib, Seaborn and how to use these libraries to implement Machine learning models.

Checkout the Detailed Review of Best Professional Certificate in Data Science with Python.

Remember, Data Science requires a lot of patience, persistence, and practice. So, start learning today.

Join a LinkedIn Community of Python Developers

Leave a Comment

Your email address will not be published.

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

Scroll to Top