Linear search on list or tuples in Python

Linear search is a simple algorithm that searches for an element by checking each item in a sequence from start to end. In Python, we can implement linear search for both lists and tuples using the same approach since both are iterable sequences.

A linear search starts from the first element and continues until it finds the target element or reaches the end of the sequence. It's the most straightforward searching algorithm with O(n) time complexity.

How Linear Search Works

The algorithm follows these steps:

  • Start from the first element of the list or tuple
  • Compare each element with the target value
  • If a match is found, return the result
  • If no match is found after checking all elements, return "not found"

Basic Implementation

Here's a simple function that performs linear search on both lists and tuples:

# function for linear search
def linear_search(iterable, element):
    # flag for marking
    is_found = False
    # iterating over the iterable
    for i in range(len(iterable)):
        # checking the element
        if iterable[i] == element:
            # marking the flag and returning respective message
            is_found = True
            return f"{element} found"

    # checking the existence of element
    if not is_found:
        # returning not found message
        return f"{element} not found"

# initializing the list and tuple
numbers_list = [1, 2, 3, 4, 5, 6]
numbers_tuple = (1, 2, 3, 4, 5, 6)

print("List:", linear_search(numbers_list, 3))
print("List:", linear_search(numbers_list, 7))
print("Tuple:", linear_search(numbers_tuple, 3))
print("Tuple:", linear_search(numbers_tuple, 7))
List: 3 found
List: 7 not found
Tuple: 3 found
Tuple: 7 not found

Optimized Version with Index

Here's an improved version that returns the index of the found element:

def linear_search_with_index(iterable, element):
    for i in range(len(iterable)):
        if iterable[i] == element:
            return f"{element} found at index {i}"
    return f"{element} not found"

# testing with different data types
fruits = ['apple', 'banana', 'orange', 'grape']
coordinates = (10, 20, 30, 40, 50)

print(linear_search_with_index(fruits, 'orange'))
print(linear_search_with_index(coordinates, 30))
print(linear_search_with_index(fruits, 'mango'))
orange found at index 2
30 found at index 2
mango not found

Using Python's Built-in Methods

Python provides built-in methods for searching that are more efficient:

# using 'in' operator
numbers = [10, 20, 30, 40, 50]
search_value = 30

if search_value in numbers:
    print(f"{search_value} found")
else:
    print(f"{search_value} not found")

# using index() method with exception handling
try:
    index = numbers.index(search_value)
    print(f"{search_value} found at index {index}")
except ValueError:
    print(f"{search_value} not found")
30 found
30 found at index 2

Comparison

Method Returns Best For
Custom linear search Custom message Learning algorithm concepts
in operator True/False Quick existence check
index() method Index or exception Finding position

Conclusion

Linear search works identically on lists and tuples in Python. While custom implementation helps understand the algorithm, Python's built-in in operator and index() method are more efficient for practical use.

Updated on: 2026-03-25T12:20:49+05:30

4K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements