In this Python tutorial, you will learn how to use list.index() method to find the index of specified element in the list, when there is one occurrence, multiple occurrences, or no occurrence of the specified element in the list with example programs.

Python List index() – Get index of element

Python List index() method returns the index of the specified element, that is passed as argument, in the list.

If the element is not at all present in the list, then it throws ValueError.

Syntax

The syntax to call index() method on a list myList is

myList.index(element)

where

  • myList is the list of elements.
  • index is the name of the method.
  • element is the one whose index in the list is to be found.
ADVERTISEMENT

Examples

1. list.index() – Single Occurrence of element in the list

In this example, we will take a list of strings myList, and find the index of 'mango' in this list.

The following screenshot depicts the list and how index() method finds a match for the element and returns its index.

Python List index()

main.py

myList = ['apple', 'banana', 'cherry', 'orange', 'mango', 'kiwi']
index = myList.index('mango')
print(f'index : {index}')
Try Online

Output

index : 4

2. list.index() – Multiple occurrences of element in the list

When the element, whose index we are trying to find, occurs multiple times in the list, index() method returns the index of first match and ignores the other occurrences.

Python List index() - Multiple Occurrences

In the following program, we take a list where element 'mango' occurs twice in the list. We find the index of the element 'mango' in the list, using list.index() method.

main.py

myList = ['apple', 'banana', 'mango', 'cherry', 'orange', 'mango', 'kiwi']
ind = myList.index('mango')
print(ind)
Try Online

Output

2

3. list.index() – Get all occurrences of element

You can use list.index() along with slicing technique to get all the occurrences of the element in the list.

In the following program, we have list of strings, and we shall find all the occurrences of the string mango in the list.

The logic is that, each time we get the index of element in the list, we slice the list from that index and find the next occurrence of the element. We collect these indexes in a list.

main.py()

myList = ['apple', 'banana', 'mango', 'cherry', 'orange', 'mango', 'kiwi']
element = 'mango'
indexes = []

while True:
    try:
        slicingPos = 0 if len(indexes) == 0 else indexes[len(indexes)-1]+1
        index = myList[slicingPos:].index(element)
        indexes.append(index+slicingPos)
    except ValueError as e:
        break

print(indexes)
Try Online

Output

[2, 5]

Reference tutorials for the above program

4. list.index() – No occurrences of specified element in the list

When there is no occurrence of element, whose index we are trying to find in the list, list.index() throws ValueError.

main.py

myList = ['apple', 'banana', 'cherry', 'orange', 'mango', 'kiwi']
index = myList.index('watermelon')
print(index)
Try Online

Output

Traceback (most recent call last):
  File "d:/workspace/python/main.py", line 2, in <module>
    ind = myList.index('watermelon')
ValueError: 'watermelon' is not in list

This could terminate our program execution. So, how do we handle this?

Prior to making a call to index() method, check if element is present in the list.

main.py

myList = ['apple', 'banana', 'cherry', 'orange', 'mango', 'kiwi']
element = 'watermelon'

if element in myList:
    ind = myList.index('watermelon')
    print(ind)
else:
    print(element, 'not in the list.')
Try Online

Output

watermelon not in the list.

Reference tutorials for the above program

Or you can also use try except to handle ValueError thrown by list.index() method.

Python Program

myList = ['apple', 'banana', 'cherry', 'orange', 'mango', 'kiwi']
element = 'watermelon'

try:
    ind = myList.index('watermelon')
    print(ind)
except ValueError as e:
    print(element, 'not in the list.')
Try Online

Output

watermelon not in the list.

Conclusion

In this Python Tutorial, we learned the syntax of list.index() method, and how to use it in different scenarios in your Python programs where we need to find the index of specified element in the given list.