Linear Search in Python
Check each item from left to right until you find a match. Simple, and it works on unsorted lists.
- Looping with enumerate to get indexes
- Returning the index or -1
- When linear search is the right choice
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.
for index, value in enumerate(values):🧥 Check each pocket in orderif value == target:🔑 Are the keys here?return index✅ Found them: stop searchingreturn -1❌ Checked every pocket: not foundLinear 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
- Start at index 0.
- Compare the current item with the target.
- Return its index when equal; otherwise continue.
- Return -1 after the final item when no match exists.
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 valuesdoes 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
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))
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.
- Try empty, single-item, and duplicate-value inputs.
- Print state after every step to observe the algorithm.
- Benchmark 100, 1,000, and 10,000 items, then compare another approach.
Common mistakes
Returning -1 inside the loop
The search gives up after the first non-matching item.
for index, value in enumerate(values):
if value == target:
return index
return -1
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.
- First Negative Index Find where the first negative number appears. Easy
- Array Minimum Find the smallest value in a list. Easy