Python 3 - List index() Method


Advertisements

Description

The index() method returns the lowest index in list that obj appears.

Syntax

Following is the syntax for index() method −

list.index(obj)

Parameters

obj − This is the object to be find out.

Return Value

This method returns index of the found object otherwise raise an exception indicating that value does not find.

Example

The following example shows the usage of index() method.

#!/usr/bin/python3

list1 = ['physics', 'chemistry', 'maths']
print ('Index of chemistry', list1.index('chemistry'))
print ('Index of C#', list1.index('C#'))

Result

When we run above program, it produces the following result −

Index of chemistry 1
Traceback (most recent call last):
   File "test.py", line 3, in <module>
      print ('Index of C#', list1.index('C#'))
ValueError: 'C#' is not in list
python_lists.htm
Advertisements