Python 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 search for.
- start -- Optional, the starting position of the search.
- end -- Optional, the ending position of the search.
Return Value
This method returns the index position of the searched object; if the object is not found, it raises an exception.
Example
The following example demonstrates the usage of the index() function:
Example
#!/usr/bin/python
# -*- coding: UTF-8 -*-
aList = [123, 'xyz', 'example', 'abc']
print "xyz index position: ", aList.index( 'xyz' )
print "example index position : ", aList.index( 'example', 1, 3 )
# -*- coding: UTF-8 -*-
aList = [123, 'xyz', 'example', 'abc']
print "xyz index position: ", aList.index( 'xyz' )
print "example index position : ", aList.index( 'example', 1, 3 )
The output of the above example is as follows:
xyz 索引位置: 1 example 索引位置 : 2Other Extensions
Python List