Concepts / Finding extremes in data

Finding extremes in data

Loops scan through data by following a three-part structure: initialize a tracking variable, compute on each item, and inspect the result.

  • Programming
Interactive lab

Try it: Loop Tracer

How a Python for loop visits each item in turn, and how an accumulator variable (a total, a count, a best-so-far or a result list) changes on every iteration.

How it works

  1. Initialise the accumulator before the loop.
  2. Each iteration, the loop variable takes the next value from the list.
  3. An if inside the loop decides whether to update the accumulator.
  4. After the last item the loop ends and the accumulator holds the answer.

Default run (13 steps): values = [4, 9, 2, 7, 5]. Run the loop one statement at a time. … The loop has used every value. print(total) shows 27.

Simplified: A model of five fixed loop programs (sum, count, maximum, minimum, filter) run on your list — it steps the program exactly as Python would, but it does not run Python.

Educational simulation

Loading the simulation…

Why Scan Data

When a collection contains numbers, test scores, temperatures, or file contents, you may need to find something specific: the largest value, the smallest value, or whether a particular item exists. Checking every item manually is tedious and error-prone. A loop lets you examine each item systematically while keeping track of what you have found so far.

Definition

Finding extremes in data uses a tracking variable that records the best result found so far. As the loop progresses, the tracking variable changes only when the current item meets the condition for replacing that result.

Key point

The central mental model is not just “the loop visits every item.” It is “the loop visits every item while the tracking variable summarizes the result found so far.”

Tracking the Best So Far

Consider the generated data sequence 7, 3, 11, 9 while looking for the largest value. The first value gives the tracking variable an initial candidate. The next value is compared with that candidate. If it is not larger, the candidate stays the same. When 11 appears, it is larger than the current candidate, so the tracking variable changes. The final 9 does not replace 11 because it is not larger.

tracking = 7first item3current itemtracking = 73 is not larger11current itemtracking = 1111 is larger9current itemtracking = 11final valuescancomparescanreplacescankeep
As each item is scanned, when does the tracking variable change and what value does it hold afterward?
Worked example

Finding the largest value

Scan the generated sequence 7, 3, 11, 9 and track the largest value found so far.

  1. 1
    Initialize

    Start the tracking variable with the first candidate, 7.

  2. 2
    Process 3

    Compare 3 with the current tracking value, 7. Because 3 is not larger, keep 7.

  3. 3
    Process 11

    Compare 11 with 7. Because 11 is larger, replace the tracking value with 11.

  4. 4
    Process 9

    Compare 9 with 11. Because 9 is not larger, keep 11.

  5. 5
    Inspect

    After every item has been processed, inspect the tracking variable.

Result

The final tracking value is 11.

What do you think happens?

After scanning 7, 3, 11, and 9 while looking for the largest value, what remains in the tracking variable?

  • 7

  • 9

  • 11

  • The tracking variable is empty

Reveal answer
Answer:

11

The tracking variable changes from 7 to 11 when 11 is encountered. The final value, 9, is not larger than 11, so the tracking variable remains 11.

The Three-Part Loop

Loops that search through data follow a three-part structure. First, initialize a tracking variable. Next, compute on each item as the loop progresses, usually by comparing the current item with the tracked result and updating the tracking variable when the condition is met. Finally, inspect the result after the loop has examined the collection.

Initializeset tracking variableNext itemcurrent data itemComparedoes condition hold?Updatereplace tracked valueKeepretain tracked valueInspect resultafter collection endsbegin scanprocesscondition metcondition not metcontinuecontinueno items remain
What happens first, what computation repeats for each item, and when is the final tracked value inspected?
StageQuestion to askPurpose
InitializationWhat value does tracking start with?Provide the first candidate result
ComputationWhat happens for each item?Compare the current item and update when the condition is met
InspectionWhat is the tracking value after the scan?Use the final result

The three stages of a data-scanning loop

The same structure applies beyond maximum searches. To find a minimum, the replacement comparison changes so a smaller current item can become the new tracked result. Counting and summing also use a tracking variable that changes as each item is processed. The specific computation changes, but the broader pattern remains: initialize, compute in the loop, and inspect the result.

Choosing the Replacement

The current item does not automatically replace the tracking variable. Replacement depends on the condition being tested. For a maximum search, a current item replaces the tracked value when it is larger. For a minimum search, a current item replaces the tracked value when it is smaller. The comparison operator therefore determines which extreme survives the scan.

Current itemnew valueTracked valuebest so farConditionmaximum or minimumCurrent itemnew tracked valueTracked valuevalue remainscomparecomparecondition metcondition not met
How is the current item compared with the tracking variable, and what determines whether the tracking variable is replaced?
Before

Current item: 11 Tracked maximum: 7

The current item is being compared with the value found so far.

After

Current item: 11 Tracked maximum: 11

Because the current item is larger, it becomes the new tracked value.

Mistakes to Avoid

  • Common mistake

    Changing the tracking variable on every iteration

    What it looks like

    Treating each newly visited item as the new extreme without comparing it with the current tracked value.

    Why it fails

    The tracking variable should update only when the condition for replacement is met.

    Do this instead

    Compare the current item with the tracked result before deciding whether to replace it.

  • Common mistake

    Using the wrong comparison direction

    What it looks like

    Using a maximum-style comparison when the task is to find a minimum.

    Why it fails

    The comparison operator determines whether larger or smaller values replace the current result.

    Do this instead

    Match the comparison with the goal: retain the appropriate extreme as the scan progresses.

  • Common mistake

    Ignoring initialization

    What it looks like

    Focusing on the repeated comparison but not checking what value the tracking variable has before the first item is processed.

    Why it fails

    Proper initialization is critical to getting the right answer.

    Do this instead

    Identify the initial candidate before tracing the loop.

  • Common mistake

    Inspecting the result too early

    What it looks like

    Treating an intermediate tracking value as the final answer before every item has been examined.

    Why it fails

    The tracked value represents the result found so far until the loop finishes.

    Do this instead

    Inspect the tracking variable after the collection has been scanned.

Practice the Trace

EASY

A generated collection contains the values 4, 12, 6, and 15. Trace a tracking variable used to find the largest value. Record the tracking value after each item is examined, then state the final value after the loop completes.

Hints
  • Begin by identifying the initial candidate.
  • For each later item, compare it with the current tracked value.
  • Replace the tracked value only when the current item is larger.
Worked example

Tracing a second maximum search

Find the final tracked value for the generated sequence 4, 12, 6, 15.

  1. 1
    Start with 4

    The first candidate makes the tracking value 4.

  2. 2
    Process 12

    12 is larger than 4, so the tracking value becomes 12.

  3. 3
    Process 6

    6 is not larger than 12, so the tracking value remains 12.

  4. 4
    Process 15

    15 is larger than 12, so the tracking value becomes 15.

  5. 5
    Inspect

    After all items are processed, inspect the final tracking value.

Result

The final tracking value is 15.

Pattern Summary

  1. 1
    A data-scanning loop initializes a tracking variable, computes on each item, and inspects the result after the scan.
  2. 2
    The tracking variable represents the best result found so far, not necessarily the final answer until the loop completes.
  3. 3
    The tracking variable changes only when the current item satisfies the replacement condition.
  4. 4
    Initialization and the comparison direction are both critical to finding the correct maximum, minimum, count, sum, or other scanned result.
  5. 5
    To predict the final state, trace each item in order and record whether the tracking variable changes.

At a glance

  • Loops find extremes by scanning each item while maintaining a tracking variable.
  • The three stages are initialization, computation during the loop, and inspection after the loop.
  • A tracking variable changes only when the current item meets the comparison condition.
  • Tracing the tracking variable after every item makes the final result predictable.
  • The same pattern extends to maximums, minimums, counting, summing, and other data-scanning tasks.

Related concepts

More concepts starting with F