Linear Search in Python
By Gaurav Jain on January 6, 2018
Return the index of a number in the given list/array
def linear_search(alist, num):
for i, elem in enumerate(alist):
if elem == num:
return i
return None # If number doesn't exist in the list
Usage:
>>> mylist = [1, 2, 3, 4, 5, 6, 7]
>>> num = 3 # number to be searched
>>> linear_search(mylist, num)
2
>>>
This code can be found on github [Python-DSA] repository.
If you find anything wrong or has a better solution, please feel free to create issues on this repo.