Linear Search in Python – How to Implement Linear Search in Python
Linear search also known as sequential search is the simple search algorithm that looks for elements in unsorted list by examining each element in the list one by one. It is the most basic and straightforward search algorithm but it can be inefficient for large lists. In this article we will discuss various ways to implement the linear search algorithm in Python.
Linear Search in Python Using a for loop:
The most common way to implement the linear search algorithm is using a loop. In this method we traverse the entire list and check if the current element matches the target element. If a match is found we return the index of the element. If we reach the end of the list without finding the element we will return -1. Here is example implementation using a for loop
def linear_search(arr, target):
for i in range(len(arr)):
if arr[i] == target:
return i
return -1
Here we pass the list arr
and the target element target
as arguments to the function. We then use a for
loop to iterate over each element in the list. If we find the target element we return the index of the element. Otherwise we return -1.
Linear Search in Python Using the while loop:
We can also implement the linear search algorithm using a while
loop. In this below method we keep iterating until we find the target element or we reach the end of the list. Here is an example implementation:
def linear_search(arr, target):
i = 0
while i < len(arr):
if arr[i] == target:
return i
i += 1
return -1
In above implementation, we use a while
loop to iterate over the list. We start by initializing the index variable i
to 0. We then check if the current element matches the target element. If a match is found we return the index of the element. Otherwise we increment the index variable and continue iterating. If we reach the end of the list without finding the element, we return -1.
Linear Search in Python Using list comprehension:
Another way to implement the linear search algorithm is by using list comprehension. In this method we create a new list of indices where the target element is found and return the first index if it exists. Here is an example implementation
def linear_search(arr, target):
indices = [i for i in range(len(arr)) if arr[i] == target]
return indices[0] if indices else -1
In above implementation we use list comprehension to create a new list of indices where the target element is found. We check if the list is empty. If it is not empty we return the first index. Otherwise we will return -1.
Linear Search in Python Using the in
keyword:
Python provides the in
keyword which can be used to check if element is present in a list. We use this keyword to implement the linear search algorithm as well. Here is an example implementation
def linear_search(arr, target):
if target in arr:
return arr.index(target)
else:
return -1
In above implementation we use the in
keyword to check if the target element is present in the list. If it is we return the index of the element using the index
method. Otherwise we will return -1.