How to implement linear search on a list in Python

1 Answer

0 votes
def linearsearch(lst, ln, n):
    for i in range(0, ln):
        if (lst[i] == n):
            return i
    return -1

lst = [1, 2, 3, 4, 5, 8, 20, 30]

if (linearsearch(lst, len(lst), 8) == -1):
    print("Element not found")
else:
    print("Element found")


 
 
 
 
'''
run:
 
Element found
 
'''

 



answered Apr 30, 2021 by avibootz
...