Python List index() method
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
- x-- the object to find.
- start-- optional, the starting position of the search.
- end-- optional, the ending position of the search.
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'
]
"xyz index position: "
,
aList.
index
(
'xyz'
)
"runoob index position: "
,
aList.
index
(
'runoob'
,
1
,
3
)
The output of the above example is:
xyz 索引位置: 1
runoob 索引位置 : 2
Python Lists