Python List index()

The index() method returns the index of the specified element in the list.

Example


Syntax of List index()

The syntax of the list index() method is:

list.index(element, start, end)

list index() parameters

The list index() method can take a maximum of three arguments:

  • element - the element to be searched
  • start (optional) - start searching from this index
  • end (optional) - search the element up to this index

Return Value from List index()

  • The index() method returns the index of the given element in the list.
  • If the element is not found, a ValueError exception is raised.

Note: The index() method only returns the first occurrence of the matching element.


Example 1: Find the index of the element

Output

The index of e: 1
The index of i: 2

Example 2: Index of the Element not Present in the List

Output

ValueError: 'p' is not in list

Example 3: Working of index() With Start and End Parameters

Output

The index of e: 1
The index of i: 6
Traceback (most recent call last):
  File "*lt;string>", line 13, in 
ValueError: 'i' is not in list