Open In App

Linear Search - Python

Last Updated : 07 Nov, 2025
Comments
Improve
Suggest changes
51 Likes
Like
Report

Linear Search checks each element of a list one by one until the desired element is found or the list ends. Given an array, arr of n elements, and an element x, find whether element x is present in the array. Return the index of the first occurrence of x in the array, or -1 if it doesn’t exist.

Examples:

Input: arr[] = [10, 50, 30, 70, 80, 20, 90, 40], x = 30
Output : 2
Explanation: For array [10, 50, 30, 70, 80, 20, 90, 40], the element to be searched is 30 and it is at index 2. So, the output is 2.

Explanation: A simple approach is to do a linear search, i.e

  • Start from the leftmost element of arr[] and one by one compare x with each element of arr[].
  • If x matches with an element, return the index.
  • If x doesn't match with any of the elements, return -1.

Using Iterative Approach

This takes an array arr and a target element to search. Iterates through each element of the array and compares it with the target. If the target is found, it returns its index; otherwise, it returns -1.

Python
def linear_search(arr, x):
    for i in range(len(arr)):
        if arr[i] == x:
            return i
    return -1

arr = [10, 23, 45, 70, 11, 15]
x = 70
res = linear_search(arr, x)

if res != -1:
    print("Element found at index:", res)
else:
    print("Element not found in the array")

Output
Element found at index: 3

Using Recursive Approach

It checks one element per recursive call and moves to the next index until it finds the target or reaches the end.

Python
def linear_search_rec(arr, x, i=0):
    if i == len(arr):
        return -1
    if arr[i] == x:
        return i
    return linear_search_rec(arr, x, i + 1)

arr = [10, 20, 30, 40, 50]
x = 30
res = linear_search_rec(arr, x)

if res != -1:
    print("Element", x, "found at index", res)
else:
    print("Element", x, "not found in the list")

Output
Element 30 found at index 2

Explanation:

  • Base case: If index equals the list length, return -1 (element not found).
  • If current element matches x, return its index.
  • Otherwise, call the function recursively for the next index.

Please refer complete article on Linear Search and Difference Between Recursive and Iterative Algorithms for more details!

Using RegEx Method

This approach is useful when the list contains strings, and we want to search for a pattern (substring) using regular expressions.

Python
import re

def regex_search(lst, pat):
    reg = re.compile(pat)
    for i, ele in enumerate(lst):
        if reg.search(ele):
            return "Pattern found in element '" + ele + "' at index " + str(i)
    return "Pattern not found in the list"

lst = ["apple", "banana", "cherry", "date", "elderberry"]
pat = "an"
res = regex_search(lst, pat)
print(res)

Output
Pattern found in element 'banana' at index 1

Explanation:

  • re.compile(pat) compiles the search pattern.
  • Each list element is checked using reg.search(ele).
  • Returns the matching element and its index if found; otherwise, returns a message saying not found.

Python Program to implement Linear Search

Explore