Python – Get Index or Position of Item in List

Python – Find Index or Position of Element in a List

To find index of the first occurrence of an element in a given Python List, you can use index() method of List class with the element passed as argument.

index = mylist.index(element)

The index() method returns an integer that represents the index of first match of specified element in the List.

You can also provide start and end positions of the List, where the search has to happen in the list.

Following is the syntax of index() function with start and end positions.

index = mylist.index(x, [start[,end]])

start parameter is optional. If you provide a value for start, then end is optional.

We shall look into examples, where we go through each of these scenarios in detail.

Python - Index or Position of Specific Element in a List

Example 1: Find Index of item in List

In the following example, we have taken a List with numbers. Using index() method we will find the index of item 8 in the list.

Python Program

mylist = [21, 5, 8, 52, 21, 87]
item = 8

#search for the item
index = mylist.index(item)

print('The index of', item, 'in the list is:', index)
Run this program ONLINE

Output

The index of 8 in the list is: 2

The element is present at 3rd position, so mylist.index() function returned 2.

Example 2: Find Index of Item in List – start, end

In the following example, we have taken a List with numbers. Using index() method we will find the index of item 8 in the list. Also, we shall pass start and end. index() function considers only those elements in the list starting from start index, till end position in mylist.

Python Program

mylist = [21, 8, 67, 52, 8, 21, 87]
item = 8
start=2
end=7

#search for the item
index = mylist.index(item, start, end)

print('The index of', item, 'in the list is:', index)
Run this program ONLINE

Output

The index of 8 in the list is: 4

Explanation

mylist = [21, 8, 67, 52, 8, 21, 87]
                 ----------------- only this part of the list is considered
                         ^ index finds the element here
          0   1   2   3  4 => 4 is returned by index()  

Example 3: Find Index of Item – Item has multiple occurrences in List

A Python List can contain multiple occurrences of an element. In such cases, only the index of first occurrence of specified element in the list is returned.

Python Program

mylist = [21, 5, 8, 52, 21, 87, 52]
item = 52

#search for the item
index = mylist.index(item)

print('The index of', item, 'in the list is:', index)
Run this program ONLINE

Output

The index of 52 in the list is: 3

The element 52 is present two times, but only the index of first occurrence is returned by index() method.

Let us understand how index() method works. The function scans the list from starting. When the item matches the argument, the function returns that index. The later occurrences are ignored.

Example 4: Find Index of Item in List – Item not present

If the element that we are searching in the List is not present, you will get a ValueError with the message item is not in list.

In the following program, we have taken a list and shall try to find the index of an element that is not present in the list.

Python Program

mylist = [21, 5, 8, 52, 21, 87, 52]
item = 67

#search for the item/element
index = mylist.index(item)

print('The index of', item, 'in the list is:', index)
Run this program ONLINE

Output

Traceback (most recent call last):
  File "example.py", line 5, in <module>
    index = mylist.index(item)
ValueError: 67 is not in list

As index() can throw ValueError, use Python Try-Except while using index(). In the following example, we shall learn how to use try-except statement to handle this ValueError.

Python Program

mylist = [21, 5, 8, 52, 21, 87, 52]
item = 67

try:
	#search for the item
	index = mylist.index(item)
	print('The index of', item, 'in the list is:', index)
except ValueError:
	print('item not present')
Run this program ONLINE

Output

item not present

The item, whose index we are trying to find, is not present in the list. Therefore, mylist.index(item) throws ValueError. except ValueError: block catches this Error and the corresponding block is executed.

Summary

In this Python Tutorial, we learned how to find the index of an element/item in a list, with the help of well detailed examples.