Reading preferences
Concept

Linear Search

Look through items one at a time, stop at the first match, and know what happens when nothing matches.

search, linear search, sequential search, first match, unsorted

Find a card without knowing its place

You have cards [7, 2, 9, 4] and want to find 9. Look at 7, then 2, then 9. When the card matches, stop. This is linear search, also called sequential search: check one item after another.

The target is the value you are trying to find. Our output is its first matching index, not the value itself.

Why we need it and where it is used

Not every row is sorted. If a small unsorted list contains a matching name, code, or number, visiting items one by one is a straightforward way to find it. It is also useful when each item needs a custom match check.

Searching once does not always justify sorting the whole collection first. For many searches on a large collection, a different structure or strategy may be better.

Follow the comparisons

Find target 9 in [7, 2, 9, 4]:

Index checkedValueDoes it equal 9?Decision
07NoContinue
12NoContinue
29YesReturn index 2

There is no need to inspect 4 because we asked for the first match. If the target were 6, we would inspect all four items and report no match.

See the reasoning

Try Linear Search one decision at a time

Find the target by checking the numbered cards. Index 0 is the first position. This is a teaching demonstration, not live Python execution.

The cards do not need to be sorted. This search stops at the first matching index.

Target: 9

  1. 7Index 0Not checked
  2. 2Index 1Not checked
  3. 9Index 2Not checked
  4. 4Index 3Not checked

Start

We will check the cards from left to right. The first matching index is the answer.

Why the result is correct

Before checking an item, every earlier item has already failed to match. Therefore a match at this index is the first match. If the loop ends, every item has failed, so reporting absence is correct.

The loop advances through a finite list, so it stops even when the target is missing. An empty list immediately reports absence.

Python illustration

def linear_search(values, target):
    for index, value in enumerate(values):
        if value == target:
            return index
    return None

print(linear_search([7, 2, 9, 4], 9))
print(linear_search([7, 2, 9, 4], 6))
print(linear_search([7, 2, 2], 2))

Prepared output — not live execution:

2
None
1

== asks whether the values match. return index stops the function as soon as they do. The final return None is outside the loop: putting it inside would wrongly stop after the first non-match.

Time and extra storage

If the first item matches, the search takes O(1) time in the best case. If the target is absent or matches only at the end, it takes O(n) in the worst case, with O(1) extra storage under the usual fixed-cost model.

Text equality may inspect several code points, so a "comparison" is not automatically constant-cost for long strings. The function returns one index, not a list of all matches.

Common mistakes and what comes next

Index 0 is a real answer. A caller should test result is not None rather than treating a zero index as false and claiming the target was absent.

Do not discard half an unsorted row because one value looks too large. That shortcut needs an ordering guarantee. Learn how Binary Search uses that guarantee to narrow the search.

Reflect on the concept

Four fixed questions to reinforce the reasoning. Answers are not saved or scored.

1. Must a list be sorted before using this linear search?
2. What index does our search return for target 2 in [7, 2, 2]?
3. What does our search return when no item matches?
4. When the target is absent from n items, how many equality checks does this search make?

References and further reading