Python Tutorial

Linear Search

The simplest search: check each element in turn until you find the target or run out.

How It Works

Linear (sequential) search walks the collection from the start, comparing each element to the target. It returns the position on the first match, or a sentinel (like −1) if the value is absent. No ordering is required — it works on any list.

CaseComparisonsTime
Best (first element)1O(1)
Averagen/2O(n)
Worst (last / absent)nO(n)

Implementation

def linear_search(arr, target):
    for i, value in enumerate(arr):
        if value == target:
            return i          # found: return index
    return -1                 # not found

data = [64, 34, 25, 12, 22, 11, 90]
print(linear_search(data, 22))   # 4
print(linear_search(data, 99))   # -1

Finding All Matches

def find_all(arr, target):
    return [i for i, v in enumerate(arr) if v == target]

print(find_all([1, 3, 3, 7, 3], 3))   # [1, 2, 4]

Pythonic Equivalents

In everyday code you rarely write the loop yourself — Python has built-ins that do linear search under the hood:

data = [64, 34, 25, 12]

print(25 in data)              # True   -> membership test
print(data.index(25))          # 2      -> raises ValueError if absent

# safe index lookup
idx = data.index(25) if 25 in data else -1

If you search the same list many times, convert it to a set once (O(n)) and then test membership in O(1) each time instead of repeated O(n) scans.

When to Use Linear Search

  • The data is small.
  • The data is unsorted and sorting first is not worth it.
  • You need every match, not just the first.
  • The structure only allows sequential access (e.g. a linked list, a stream).

If the data is sorted and you search it often, binary search (next lesson) is dramatically faster at O(log n).

Best Practices

  • Return a clear "not found" signal (−1 or None) and document it.
  • Use enumerate to get index and value together.
  • Prefer sets/dicts for repeated lookups; prefer binary search for sorted data.

Try It Yourself

Exercise 1: Return the index of the first negative number in a list, or −1.

Show solution
def first_negative(a):
    for i, v in enumerate(a):
        if v < 0:
            return i
    return -1
print(first_negative([3, 5, -2, 8]))   # 2

Exercise 2: When is linear search the right choice over binary search?

Show solution

When the data is unsorted, small, or you need every match — binary search requires sorted data.

📘 Real-World Deep Dive

Knowing <strong>DSA Linear Search (algorithms & data structures)</strong> well is what turns algorithms & data structures from a curiosity into a daily tool — you'll reach for it in nearly every real project.

Real-Life Scenario

An end-to-end usage of DSA Linear Search that you'd actually see in a data pipeline or analytics notebook.

Real-Life Example

def find(xs, target):
    for i, x in enumerate(xs):
        if x == target: return i
    return -1

print(find([4, 1, 7, 3, 9], 7))
print(find([4, 1, 7, 3, 9], 8))

Expected Output

(see source)

Common mistakes

  • Off-by-one errors in binary-search: the standard idiom is while lo <= hi with mid = (lo + hi) // 2.
  • Recursive algorithms blow the stack for n > ~10⁴; convert to iterative with an explicit stack.
  • Comparison algorithms (sorted(iterable)) are stable by default in Python — surprising for Java/C++ users.
  • Treating DSA Linear Search as a black box without reading the docs — the API has subtle defaults that bite when you scale.

🚀 Performance & Best Practices

  • Use bisect.bisect_left / bisect_right instead of writing your own binary search.
  • Convert a sorted search into a tuple-access pattern with numpy.searchsorted for huge arrays.
  • Use heapq for priority queues instead of maintaining a sorted list manually.
  • When working with algorithms & data structures, prefer vectorised / batched operations over Python loops.

🧪 Try It Yourself

  1. Reproduce the snippet on a representative slice of your own data.
  2. Profile the snippet with cProfile or timeit and find the single biggest improvement.
  3. Generalise the snippet into a small, reusable function you can drop into future projects.

FAQ: Linear Search

Common questions about this page.

What is Linear Search?

Linear Search is a DSA lesson that explains linear search in Python. The simplest search: check each element in turn until you find the target or run out. Copy the samples and run them in the Python editor. It is written for beginners who want a clear definition and working examples.

Should I run linear search examples locally for better learning?

Yes. Use the browser editor on StudyGrid for a quick check, then Download the example and run it on your computer. Local runs show real errors and the real toolchain, which is one of the fastest ways to learn linear search in this DSA Python lesson (Linear Search).

How do I use linear search in Python?

To use linear search in Python, follow the examples on this StudyGrid page. Copy a snippet, run it in the browser, then Download and run it locally for better learning. Change the values and compare the output.

What is the syntax of linear search?

This Linear Search tutorial shows linear search syntax with short Python examples. Use the code blocks in this lesson for the exact statements, then try them in your editor.

Linear Search example for beginners

Yes. This page includes a beginner linear search example you can copy and run. It is designed for searches such as "linear search for beginners", "linear search example", and "how to use linear search".

What are common mistakes with linear search?

Common linear search mistakes include wrong syntax, mixing types, and skipping practice. Work through this DSA chapter in order, run every example, and check the output before moving on.

Why should I learn linear search?

Linear Search is used in real Python work. Learning linear search helps you write clearer programs and continue the DSA tutorial on StudyGrid.

Is Linear Search free to learn online?

Yes. You can learn linear search free on StudyGrid (studygrid.in). This chapter is part of the DSA path and includes examples, syntax, and next-step links.