Python linear search

Linear search checks elements from left to right. Returning immediately on equality finds the first occurrence.

Python
def find(values, target):
    for index, value in enumerate(values):
        if value == target:
            return index
    return -1

print(find([4, 9, 2, 9], 9))
print(find([4, 9, 2, 9], 7))
assert find([], 9) == -1
assert find([9], 9) == 0
Read the full lesson