Javatpoint Logo
Javatpoint Logo

Python List index() Method

Python index() method returns index of the passed element. This method takes an argument and returns index of it. If the element is not present, it raises a ValueError.

If list contains duplicate elements, it returns index of first occurred element.

This method takes two more optional parameters start and end which are used to search index within a limit.

Signature

Parameters

No parameter

Return

It returns None.

Let's see some examples of index() method to understand it's functionality.

Python List index() Method Example 1

Let's first see a simple example to get index of an element using index() method.

Output:

Index of p : 1

Python List index() Method Example 2

The method returns index of first element occurred, if the element is duplicate. See the following example.

Output:

Index of l : 3
Index of p : 1

Python List index() Method Example 3

The index() method throws an error ValueError if the element is not present in the list. See the example below.

Output:

index = apple.index('z')
ValueError: 'z' is not in list

Python List index() Method Example 4

Apart from element, the method takes two more optional parameters start and end. These are used to find index within a limit.

Output:

Index of p : 2
Index of a : 0

Next TopicPython Lists




Please Share

facebook twitter google plus pinterest

Learn Latest Tutorials


Preparation


B.Tech / MCA