Python Algorithms

Linear Search in Python

Check each item from left to right until you find a match. Simple, and it works on unsorted lists.

  • Intermediate
  • 2 min read
  • 1 runnable example
What you'll learn
  • Looping with enumerate to get indexes
  • Returning the index or -1
  • When linear search is the right choice
Find 8 in [14, 3, 27, 8, 19]
14[0] ✗
3[1] ✗
27[2] ✗
8[3] ✓
19[4]
↑ returns 3

What is linear search?

The problem

Sometimes the data is not sorted, or you only search it once: a short list of names, today's orders, the first error in a log. You need the simplest search that always works.

Think of it like this

Linear search is like looking for your keys in a row of coat pockets. You check the first pocket, then the next, one by one, and stop as soon as you find them. If you reach the last pocket, the keys are not there.

From checking pockets to code
for index, value in enumerate(values):🧥 Check each pocket in order
if value == target:🔑 Are the keys here?
return index✅ Found them: stop searching
return -1❌ Checked every pocket: not found
Check one item at a time; stop at the first match.
In one sentence

Linear search checks the items of a collection one after another until it finds the target, taking up to O(n) steps but needing no sorting.

Linear search checks each item from the beginning until it finds the target or reaches the end. The data does not need to be sorted first.

How the algorithm works

  1. Start at index 0.
  2. Compare the current item with the target.
  3. Return its index when equal; otherwise continue.
  4. Return -1 after the final item when no match exists.
Complexity: O(n) average/worst-case time and O(1) extra space.

When should you use it?

  • Unsorted or small collections.
  • Searching once, where sorting first would cost more.
  • Finding the first item that matches a condition, not just an exact value.

When not to use it

  • Many searches on the same large data: sort it once and use binary search, or build a set or dictionary.
  • Checking only whether a value is present: target in values does the same with less code.

Linear Search in Python visualizer

O(n)

Use Play or Step to follow each comparison and data movement.

Example code

Run code →
main.py
def linear_search(values, target):
    for index, value in enumerate(values):
        if value == target:
            return index
    return -1

print(linear_search([14, 3, 27, 8, 19], 8))
Get a Run button for this example

Result of running the code

3

Code explanation

Enumerate provides each index and value. The search returns immediately when it finds the target. Worst-case time complexity is O(n).

Change the values and run the program in the CodeUtility online Python compiler without installing Python locally.

Practice exercises

Modify the runnable example with the exercises below to build understanding beyond copying the result.

  1. Try empty, single-item, and duplicate-value inputs.
  2. Print state after every step to observe the algorithm.
  3. Benchmark 100, 1,000, and 10,000 items, then compare another approach.
Run in the Python compiler →

Common mistakes

Returning -1 inside the loop

The search gives up after the first non-matching item.

Problem
for index, value in enumerate(values):
    if value == target:
        return index
    return -1
Fix
for index, value in enumerate(values):
    if value == target:
        return index
return -1

Finished this lesson?

Mark it as learned to track your progress.

Practice with a challenge

Use what this lesson covers to solve a real problem. Each challenge runs in your browser with instant tests and an AI review.

All challenges