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.
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
- Initialise the accumulator before the loop.
- Each iteration, the loop variable takes the next value from the list.
- An if inside the loop decides whether to update the accumulator.
- 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.
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.
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.
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.
Finding the largest value
Scan the generated sequence 7, 3, 11, 9 and track the largest value found so far.
- 1Initialize
Start the tracking variable with the first candidate, 7.
- 2Process 3
Compare 3 with the current tracking value, 7. Because 3 is not larger, keep 7.
- 3Process 11
Compare 11 with 7. Because 11 is larger, replace the tracking value with 11.
- 4Process 9
Compare 9 with 11. Because 9 is not larger, keep 11.
- 5Inspect
After every item has been processed, inspect the tracking variable.
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?
Reveal 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.
| Stage | Question to ask | Purpose |
|---|---|---|
| Initialization | What value does tracking start with? | Provide the first candidate result |
| Computation | What happens for each item? | Compare the current item and update when the condition is met |
| Inspection | What 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 item: 11 Tracked maximum: 7
The current item is being compared with the value found so far.
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 likeTreating each newly visited item as the new extreme without comparing it with the current tracked value.
Why it failsThe tracking variable should update only when the condition for replacement is met.
Do this insteadCompare the current item with the tracked result before deciding whether to replace it.
Common mistake Using the wrong comparison direction
What it looks likeUsing a maximum-style comparison when the task is to find a minimum.
Why it failsThe comparison operator determines whether larger or smaller values replace the current result.
Do this insteadMatch the comparison with the goal: retain the appropriate extreme as the scan progresses.
Common mistake Ignoring initialization
What it looks likeFocusing on the repeated comparison but not checking what value the tracking variable has before the first item is processed.
Why it failsProper initialization is critical to getting the right answer.
Do this insteadIdentify the initial candidate before tracing the loop.
Common mistake Inspecting the result too early
What it looks likeTreating an intermediate tracking value as the final answer before every item has been examined.
Why it failsThe tracked value represents the result found so far until the loop finishes.
Do this insteadInspect the tracking variable after the collection has been scanned.
Practice the Trace
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.
Tracing a second maximum search
Find the final tracked value for the generated sequence 4, 12, 6, 15.
- 1Start with 4
The first candidate makes the tracking value 4.
- 2Process 12
12 is larger than 4, so the tracking value becomes 12.
- 3Process 6
6 is not larger than 12, so the tracking value remains 12.
- 4Process 15
15 is larger than 12, so the tracking value becomes 15.
- 5Inspect
After all items are processed, inspect the final tracking value.
The final tracking value is 15.
Pattern Summary
- 1A data-scanning loop initializes a tracking variable, computes on each item, and inspects the result after the scan.
- 2The tracking variable represents the best result found so far, not necessarily the final answer until the loop completes.
- 3The tracking variable changes only when the current item satisfies the replacement condition.
- 4Initialization and the comparison direction are both critical to finding the correct maximum, minimum, count, sum, or other scanned result.
- 5To 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.