01 / 20

Linear Search
in Python

Learn how to search through a list one element at a time, understand its time complexity, and implement it from scratch in Python.

Python

What Is Searching?

Searching is one of the most fundamental operations in computer science. Given a collection of data (like a Python list), searching means finding whether a specific target value exists in that collection, and if so, where it is located.

Meet Linear Search

Linear search (also called sequential search) is the simplest search algorithm. It checks every element in the list one by one, from the first element to the last, until it finds the target or runs out of elements.

✓ Simple
No sorting required: works on any list, in any order.
★ Universal
Works on all data types: numbers, strings, objects.
→ Sequential
Examines elements left to right, index \( 0 \) to \( n-1 \).

How It Works Visually

Searching for the value 7 in the list [3, 8, 1, 7, 5]. We check each element left to right:

0 1 2 3 4 3 3 ≠ 7 8 8 ≠ 7 1 1 ≠ 7 7 7 = 7 ✓ 5 skipped scan direction → Found at index 3

The Algorithm

1
Start at index \( i = 0 \): the first element of the list.
2
Compare the element at index \( i \) with the target value.
3
If they match: return \( i \) as the index where the target was found.
4
If they don't match: increment \( i \) by 1 and repeat step 2.
5
If \( i \) reaches \( n \) (end of list): return \( -1 \), meaning the target was not found.

Time Complexity

Best Case
\( O(1) \): Target is the first element. One comparison.
Average Case
\( O(n) \): On average we check about \( \frac{n}{2} \) elements.
Worst Case
\( O(n) \): Target is last, or not in the list at all.

Linear vs Binary Search

Linear Search Binary Search
Requires sorted list? No Yes
Time complexity \( O(n) \) \( O(\log n) \)
Strategy Check every element Divide and conquer
Simplicity Very simple More complex
Best for Small / unsorted lists Large sorted lists

How to Implement It

1
Define the function with two parameters: the list to search and the target value to find.
2
Loop through indices using for i in range(len(lst)) to visit each element.
3
Compare each element to the target: if lst[i] == target.
4
Return the index immediately when a match is found: return i.
5
Return \( -1 \) after the loop if no match was found (target absent).

Basic Implementation

def linear_search(lst, target): for i in range(len(lst)): if lst[i] == target: return i return -1

Example 1: Target Found

lst = [10, 24, 6, 15, 33] target = 15
10 i=0: skip 24 i=1: skip 6 i=2: skip 15 i=3: match! 33 return 3

Example 2: Target Not Found

lst = [4, 9, 2, 11] target = 7
4 4 ≠ 7 9 9 ≠ 7 2 2 ≠ 7 11 11 ≠ 7 return -1 (not found)

Example 3: Searching Strings

Linear search is not limited to numbers. It works on any data type that supports equality comparison (==).

fruits = ["apple", "banana", "cherry", "date"] linear_search(fruits, "cherry") # returns 2 linear_search(fruits, "fig") # returns -1

Pythonic Variation

Python's enumerate() function gives you both the index and the value in each loop iteration, making the code cleaner:

def linear_search(lst, target): for i, value in enumerate(lst): if value == target: return i return -1

Return Index vs Boolean

Sometimes you only need to know if the target exists, not where. Here are both patterns:

Return Index
Returns the position (or \( -1 \)). Use when you need the location.
Return Boolean
Returns True / False. Use when you only need existence.
# Boolean version def contains(lst, target): for value in lst: if value == target: return True return False

Common Mistakes

✕ Returning inside the loop too late
Putting return -1 inside the loop causes it to return after the first non-match instead of checking all elements.
✕ Using wrong comparison
Using is instead of ==: is checks identity (same object), not equality (same value).
✕ Off-by-one with range
Using range(1, len(lst)) skips index 0, missing the first element entirely.

Summary

Property Linear Search
Strategy Check each element sequentially
Input requirement Any list (sorted or unsorted)
Best case \( O(1) \)
Worst / Average case \( O(n) \)
Space complexity \( O(1) \)
Returns Index of target, or \( -1 \)
Works on Numbers, strings, any comparable type

Question 1 of 5

What is the worst-case time complexity of linear search on a list of \( n \) elements?

Question 2 of 5

What does this function return?

linear_search([5, 12, 8, 3, 20], 8)

Question 3 of 5

What is wrong with this code?

def search(lst, target): for i in range(len(lst)): if lst[i] == target: return i else: return -1

Question 4 of 5

Which of the following is true about linear search?

Question 5 of 5

How many comparisons does linear search make to find 42 in [10, 42, 7, 3]?