Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Python List index() method

Python List index() method

Python Lists Python Lists


Description

The index() function finds the index of the first match of a value in a list.

Syntax

index() method syntax:



list.index(x[, start[, end]])

Parameters

Return Value

This method returns the index of the object; if the object is not found, it raises an exception.

Example

The following example shows how to use the index() function:

Example

#!/usr/bin/python

-- coding: UTF-8 --

aList

=

[

123

,

'xyz'

,

'runoob'

,

'abc'

]

print

"xyz index position: "

,

aList.

index

(

'xyz'

)

print

"runoob index position: "

,

aList.

index

(

'runoob'

,

1

,

3

)

The output of the above example is:



xyz 索引位置:  1

runoob 索引位置 :  2

Python Lists Python Lists