Python3 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 1
#!/usr/bin/python3
list1
=
[
'Google'
,
'Runoob'
,
'Taobao'
]
(
'Runoob index is'
,
list1.
index
(
'Runoob'
)
)
(
'Taobao index is'
,
list1.
index
(
'Taobao'
)
)
The output of the above example is:
Runoob 索引值为 1
Taobao 索引值为 2
Example 2
#!/usr/bin/python3
list1
=
[
'Google'
,
'Runoob'
,
'Taobao'
,
'Facebook'
,
'QQ'
]
Start searching from a specified position
(
'Runoob index is'
,
list1.
index
(
'Runoob'
,
1
)
)
The output of the above example is:
Runoob 索引值为 1
The Python3 list