Python list index method: query the index of an element with a specific value

table of Contents

description

Syntax and parameters

return value

Usage example

Query in the list

Query in a certain interval in the list

Precautions

The element to be queried does not exist in the list

The element to be queried exists multiple times in the list

start is greater than end

start and end are beyond the valid range of the list


 

description

The Python list.index method returns the index of the element of a certain value in the list.

 

Syntax and parameters

list.index(element, start, end)
name meaning
element The value of the element to be queried, the parameter that cannot be omitted, can be any type of object instance
start Optional integer parameter, the starting position of the search.
end Optional integer parameter, the end position of the search.

 

return value

int. The list.index method returns the index of the list where the parameter value is located.

 

Usage example

Query in the list

The index method returns the index in the list where the parameter is located.

>>> demo = ["running", "pending", "error"]
>>> demo.index("running")
0

Query in a certain interval in the list

By default, the index method searches the entire list for the value to be queried. Use the start and end parameters to search in a certain range in the list. When the end value is omitted, it means that the search starts from the index corresponding to the start value and continues to the last element of the list. When both start and end values ​​exist, it means to search from the closed interval [start, end-1].

>>> demo = ["a", "p", "p", "l", "e"]
>>> demo.index("p", 2)
2
>>> demo.index("p", 3, 4)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: 'p' is not in list

 

 

Precautions

The element to be queried does not exist in the list

When the element to be queried does not exist in the list, the index method throws a ValueError exception.

>>> demo = ["running", "pending", "error"]
>>> demo.index("finish")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: 'finish' is not in list

The element to be queried exists multiple times in the list

When the element to be queried exists multiple times in the list, the index method returns the index of the element closest to the head of the list.

>>> demo = ["running", "pending", "error", "error"]
>>> demo.index("error")
2

Therefore, when the value exists multiple times in the list, the index method can only return the index value of the element closest to the head of the list, but not the index of all matching values.

start is greater than end

Since start and end can be positive or negative, they actually represent the index subscript of the list in real logic. When the logical subscript corresponding to start is on the right or overlaps the logical subscript corresponding to end, the index method throws a ValueError exception. For example, in the following code, although the value of start (1) is greater than the value of end (-1), the logical index represented by the end value is 4, which is located to the right of start, so index can be returned correctly.

>>> demo = ["a", "p", "p", "l", "e"]
>>> demo.index("l", 1, -1)
3

The logical index indicated by the start value of the following code is on the right side of the logical index indicated by the end value, and overlaps. In these cases the index method throws a ValueError exception.

>>> demo = ["a", "p", "p", "l", "e"]
>>> demo.index("p", 3, 1)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: 'p' is not in list
>>> demo.index("p", 1, 1)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: 'p' is not in list

 

start and end are beyond the valid range of the list

The values ​​of start and end involve the following possibilities:

A. Both start and end are in the valid range of the list length;

B. start is in the valid range, but end is not;

C. start is not in the valid range, and end is in;

D. Both start and end are not in the valid range.

For scenario A, we have already discussed that if both start and end are within the valid range of length, index will return the index of the element being searched, or throw a ValueError exception.

When start and end exceed the valid range of the list length, the index method will not report an error. Instead, the start or end that exceeds the length will be regarded as the maximum length value, that is, the subscript +1 of the last element of the list.

>>> demo = ["a", "p", "p", "l", "e"]
>>> demo.index("e", 0, 400)
4

In the above code, the end value is 400, which obviously exceeds the maximum range of the list. But the start value is 0, which is within the length of the list, so the index method searches for the element "e" from the 0th element and continues to the last element.

For C and D scenarios, the index method will throw a ValueError exception.

>>> demo = ["a", "p", "p", "l", "e"]
>>> demo.index("e", 500, 2)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: 'e' is not in list
>>>
>>> demo.index("e", 400, 500)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: 'e' is not in list

 

Guess you like

Origin blog.csdn.net/TCatTime/article/details/113433769