Skip to content

Linear Search

Andrew Burke edited this page Apr 3, 2024 · 5 revisions
def linear_search(lst, target):
    for i in range(len(lst)):
        if lst[i] == target:
            return i
    return -1

print(linear_search([1, 4, 5, 2, 8], 5))  # Output: 2
print(linear_search([1, 4, 5, 2, 8], 10)) # Output: -1
Clone this wiki locally