Python Search Algorithm Example (Linear Search)
Linear search is one of the simplest ways to find a value in Python.
It works by checking items one by one from left to right until it finds a match. This makes it a great beginner example because it uses basic ideas like loops, comparisons, and return values.
Quick example #
def linear_search(items, target):
for index, value in enumerate(items):
if value == target:
return index
return -1
numbers = [4, 7, 1, 9, 3]
result = linear_search(numbers, 9)
print(result)
Output:
3
This function returns the index of the target if found. It returns -1 if the target is not in the list.
What this example teaches #
- Linear search checks each item from left to right
- It stops when it finds a match
- It works well for small lists or simple beginner programs
- It does not require the list to be sorted
How linear search works step by step #
- Start at the first item in the list
- Compare the current item to the target value
- If they match, return the position
- If they do not match, move to the next item
- If the loop ends, the target was not found
This pattern is a good way to practice Python for loops and Python if statements.
Example: find a number in a list #
A simple way to write linear search is to put it in a function. That lets you reuse the same logic with different lists and target values.
def linear_search(items, target):
for index, value in enumerate(items):
if value == target:
return index
return -1
numbers = [4, 7, 1, 9, 3]
print(linear_search(numbers, 9))
print(linear_search(numbers, 3))
print(linear_search(numbers, 8))
Output:
3
4
-1
How this code works #
enumerate(items)gives both the index and the valueif value == target:checks whether the current item matchesreturn indexstops the function as soon as the target is foundreturn -1runs only if the loop finishes without finding a match
If enumerate() is new to you, see Python enumerate() explained.
Example: find text in a list of strings #
Linear search works with strings too.
def linear_search(items, target):
for index, value in enumerate(items):
if value == target:
return index
return -1
words = ["apple", "banana", "grape", "orange"]
print(linear_search(words, "grape"))
print(linear_search(words, "pear"))
Output:
2
-1
The comparison still uses ==.
Be careful with uppercase and lowercase letters. For example, "Apple" and "apple" are not equal.
words = ["Apple", "Banana", "Grape"]
print(linear_search(words, "apple"))
Output:
-1
If you want case-insensitive matching, you can convert both values to lowercase first:
def linear_search_case_insensitive(items, target):
target = target.lower()
for index, value in enumerate(items):
if value.lower() == target:
return index
return -1
words = ["Apple", "Banana", "Grape"]
print(linear_search_case_insensitive(words, "apple"))
Output:
0
How to understand the result #
The function in this example returns an index.
- A returned index means the item was found
-1is a common “not found” value in simple examples- Some programs may return
Noneinstead
For example:
numbers = [10, 20, 30]
print(linear_search(numbers, 20))
print(linear_search(numbers, 99))
Output:
1
-1
Here:
1means20is at index1-1means99was not found
If you want more practice finding values in lists, see how to find an item in a list in Python.
When to use linear search #
Linear search is useful when:
- You have a small list
- You are learning loops and conditionals
- Your data is not sorted
- You want a simple, easy-to-read solution
It is usually not the best choice for very large lists because it may need to check every item.
For beginners, though, it is a very helpful example because it shows how programs examine data step by step.
Common beginner mistakes #
Here are some common problems when writing linear search.
Returning -1 too early #
This is a very common bug:
def linear_search(items, target):
for index, value in enumerate(items):
if value == target:
return index
return -1
This returns -1 after checking only the first item.
The fix is to put return -1 after the loop:
def linear_search(items, target):
for index, value in enumerate(items):
if value == target:
return index
return -1
Confusing the index with the value #
This function returns the position, not the item itself.
In this list:
numbers = [4, 7, 1, 9, 3]
If you search for 9, the function returns 3 because 9 is at index 3.
Using = instead of == #
This is wrong:
if value = target:
Use == for comparison:
if value == target:
Forgetting the “not found” case #
If the target is not in the list, the function still needs to return something.
That is why this line matters:
return -1
Searching for the wrong data type #
These are not the same:
"5"→ a string5→ an integer
Example:
numbers = [1, 2, 3, 4, 5]
print(linear_search(numbers, "5"))
print(linear_search(numbers, 5))
Output:
-1
4
FAQ #
What does linear search return in Python? #
In this example, it returns the index of the matching item. If the item is not found, it returns -1.
Does linear search require a sorted list? #
No. It checks items one by one, so the list does not need to be sorted.
Why use enumerate() in linear search? #
enumerate() gives both the index and the value, which makes the loop easier to read.
Can linear search be used with strings? #
Yes. You can search through a list of strings the same way you search through numbers.
Is linear search fast? #
It is simple but not very fast for large lists because it may need to check every item.