Python3 List index() Method
Description
The index() function is used to find the index position of the first matching item of a value in a list.
Syntax
Syntax of the index() method:
list.index(x[, start[, end]])
Parameters
- x -- The object to be searched for.
- start -- Optional, the starting position for the search.
- end -- Optional, the ending position for the search.
Return Value
This method returns the index position of the searched object. If the object is not found, an exception is raised.
Examples
The following examples demonstrate how to use the index() function:
Example 1
#!/usr/bin/python3
list1 = ['Google', 'Example', 'Taobao']
print ('Example index value is', list1.index('Example'))
print ('Taobao index value is', list1.index('Taobao'))
list1 = ['Google', 'Example', 'Taobao']
print ('Example index value is', list1.index('Example'))
print ('Taobao index value is', list1.index('Taobao'))
The output of the above example is as follows:
Example 索引值为 1 Taobao 索引值为 2
Example 2
#!/usr/bin/python3
list1 = ['Google', 'Example', 'Taobao', 'Facebook', 'QQ']
# Start searching from a specified position
print ('Example index value is', list1.index('Example',1))
list1 = ['Google', 'Example', 'Taobao', 'Facebook', 'QQ']
# Start searching from a specified position
print ('Example index value is', list1.index('Example',1))
The output of the above example is as follows:
Example 索引值为 1Other Extensions
Python3 Lists