Register Login

Python List Index() Method

Updated Mar 05, 2020

What is Python List Index() Method?

The index() method is a built-in function available in python. This method finds an element in the list and returns the index of the provided element. In short, it returns the index value of the element.

In python, the index starts from 0 and not 1.

But what if the same element is present more than one time in a sequence? In this case, the index method returns the index value of the first occurrence of the element.

And what if the element does not exist in the list? In this case, it returns a ValueError

Syntax of Index() Method

List_name.index(element,start,end)

Parameters of Index() Method

  1. Element: It is the item whose index value is to be searched and returned.
  2. Start: It is optional. And is the position from where the search is to be started.
  3. End: It is also optional. And is the position until where the search is to be done.

Example 1

Finding the index of the element present in a sequence

# Python3 program 
# For finding the index of element

# Initializing the list 'MyList'
MyList = ['India', 'USA', 'UK', 'Russia'];

# Index of 'USA'
MyIndex = MyList.index('USA')

# Printing the Index 
print('The Index of USA :', MyIndex)

Output

The Index of USA : 1

Explanation

Python List Index()

In the above code, at first, we initialized the list ‘MyList’. In the next line, we used the index() to find the index of ‘USA’ and stored it in the variable ‘MyIndex’. Then in the last line of the code, we printed the index of ‘USA’.  

Example 2

Finding the Index of the List That Occurs Multiple times in a Sequence

# Python3 program 
# For finding the index of element

# Initializing the list 'MyList'
MyList = ['India', 'USA', 'UK', 'Russia','UK'];

# Index of 'UK'
MyIndex = MyList.index('UK')

# Printing the Index 
print('The Index of UK :', MyIndex)

Output:

The Index of UK : 2

Explanation

In the above code, the index() function is used to find the index of the element ‘UK’. But we can see the same element occurs multiple time. So whose index will be returned?

The answer is, index of the first occurrence of the element is returned which, in this case, is 2.

Example 3

Finding the Index of the Element That Is Not Present in the List

# Python3 program
# For finding the index of element
# Initializing the list 'MyList'
MyList = ['India', 'USA', 'UK', 'Russia','UK'];
# Index of 'China'
MyIndex = MyList.index('China')
# Printing the Index 
print('The Index of UK :', MyIndex)

Output

File "index3.py", line 8, in <module>

    MyIndex = MyList.index('China')

ValueError: 'China' is not in list

Explanation

In the above code, the index() function is used to find the index of the element.

But the element whose index is to be found is not in the list. Thus a ValueError is encountered that specifies the element that is not present in the list.

Solution

Using Exception Handling to Handle the Error

# Python3 program
# For finding the index of element
# Initializing the list 'MyList'
MyList = ['India', 'USA', 'UK', 'Russia','UK'];
# Exception Handling try:
# Index of 'China'
MyIndex = MyList.index('China')
# Printing the Index
print('The Index of UK :', MyIndex)
except:
print("Provided element is not present in the list")

Output:

Provided element is not present in the list

Explanation 

If we don’t want the flow of our program to be disrupted. We can use exception handling to handle the ValueError. The try block tests the code inside for errors. And the except block handles the error. In this particular example, if the error is raised then except block is executed.  And “Provided element is not present in the list” is displayed on the screen.  

Example 4

Finding the Index of the Tuple or List in a List

# Python3 program 
# For finding the index of element
# Initializing the list 'MyList'
MyList = ['India', 'USA', 'UK', 'Russia','UK'];
# Exception Handling 
try:
	# Index of 'China'
	MyIndex = MyList.index('China')

	# Printing the Index 
	print('The Index of UK :', MyIndex)
except:
	print("Provided element is not present in the list")

Output

Index of tuple('USA','UK') : 1

The Index of List['China','Japan'] : 3

Explanation

In the above code, the index function is used to find the index of the tuple and list created inside the list ‘MyList’. since tuple is defined at index 1, we get its index as 1 and list index as 3.  


×