index_of()
Gets the index at which the first occurrence of value is found.
Implementation
Args: array: List to search. value: Value to search for. from_index: Index to search from. Returns: Index of found item or -1 if not found.
Example
index_of([1, 2, 3, 4], 2)
Expected output: 1
Alternative usage:
index_of([2, 1, 2, 3], 2, from_index=1)
Expected output: 2
Source Code
def index_of(array: List[T], value: T, from_index: int = 0) -> int:
length = len(array)
start = from_index if from_index >= 0 else max(length + from_index, 0)
for i in builtins.range(start, length):
if array[i] == value:
return i
return -1