B2.4.2 · Searching
Linear search
Python · step by step
Linear search
Check values in order; stop at the first match.
Python implementation
1def linear_search(items, target):2 for index in range(len(items)):3 if items[index] == target:4 return index5 return -1 The first item is the best case, O(1). The last item or an absent target takes O(n) time. This implementation uses O(1) auxiliary space.
Keep exploring
Return to the linear search lesson →
All B2.4 animations →