Avoid ValueError exception with List.index() in Python

This tutorial will discuss about unique ways to avoid valueerror exception with list.index() in Python.

Table Of Contents

List.index() and ValueError

In Python, the List class provides a method index(), which accepts an element as argument, and returns the index position of first occurrence of that element in List. If given element does not exists in list, then the index() method will raise the ValueError.

In the below example, we will try to get the index position of value 999 in list. As list does not contains the value 999, so it will raise the ValueError.

Let’s see the complete example,

listOfNumbers = [12, 42, 44, 68, 91, 72, 71, 81, 82]
value = 999

# find index of value in list
idx = listOfNumbers.index(value)

print('Index of 999 is :', idx)

Output

Traceback (most recent call last):
  File "temp.py", line 5, in <module>
    idx = listOfNumbers.index(value)
ValueError: 999 is not in list

Avoid ValueError exception with List.index() using if-else

To avoid the ValueError exception, while using the index() method of List, always check if the list contains the given element or not, before calling the index() method with that element. If List contains the element, then only call the index(element) method to get its index position.

In the below example, first we will check if list contains the value 999, if yes then only we will try to get its index position. As list does not contains the value 999, so control will go to the else block, where we will print a proper error message, and our application will not end abruptly.

Let’s see the complete example,

listOfNumbers = [12, 42, 44, 68, 91, 72, 71, 81, 82]

value = 999

if value in listOfNumbers:
    # find index of value in list
    idx = listOfNumbers.index(value)
    print('Index of 999 is :', idx)
else:
    print('List does not have this element')

Output

List does not have this element

Avoid ValueError exception with List.index() using try/except

Always call the List.index() method inside a try block. If list does not contains the given element, then index() method will raise ValueError exception, which will get caught in the except block. So, this is how we can handle the ValueError exception.

In the below example, we will try to get the index position of value 999 in list. As list does not contains the value 999, so it will raise the ValueError, which will get causght by the except block, and it will print the proper error message and our application will not end abruptly.

Let’s see the complete example,

listOfNumbers = [12, 42, 44, 68, 91, 72, 71, 81, 82]

value = 999

try:
    # find index of value in list
    idx = listOfNumbers.index(value)
    print('Index of 999 is :', idx)
except:
    print('List does not have this element')

Output

List does not have this element

Summary

We learned about different ways to avoid ValueError exception while using the List.index() method 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