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

Python3 List index() Method

Python3 List index() Method

Python3 Lists The Python3 list


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 1

#!/usr/bin/python3

list1

=

[

'Google'

,

'Runoob'

,

'Taobao'

]

print

(

'Runoob index is'

,

list1.

index

(

'Runoob'

)

)

print

(

'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

print

(

'Runoob index is'

,

list1.

index

(

'Runoob'

,

1

)

)

The output of the above example is:


Runoob 索引值为 1

Python3 Lists The Python3 list