Loops and Iteration
-
Accumulators in Loops
An accumulator stores a result for the part of the input a loop has processed so far.
-
Counting Events in a Loop
A counter increases when a specific event occurs. Its final value depends on both the input and the counting rule.
-
Choosing Between For and While Loops
Choose iteration over known inputs when each supplied item has work. Use a condition-driven loop when the state determines whether another step is needed.
-
Counting with Python's Range
A range represents an arithmetic progression with an excluded stop value and a nonzero step.
-
Keeping the Index While Iterating
An enumerated loop provides a position and an item together when both are needed for the same operation.
-
Iterating Over Two Sequences Together
Paired iteration reads corresponding positions and needs a policy when the two sources have different lengths.
-
Breaking and Continuing a Loop
A break exits the nearest loop. A continue skips the rest of the current iteration and proceeds according to that loop's rules.
-
Python Loop Else Clauses
A loop else clause runs when the loop completes normally, including a for loop that reads no items. A break skips it.
-
Listing Pairs with Nested Loops
Nested loops generate pairs by combining outer and inner positions. The bounds determine whether pairs repeat.
-
Off-by-One Errors in Loops
An off-by-one error usually changes the first or last position reached, which makes short traces especially useful.
-
Testing Progress in a While Loop
A condition-driven loop needs a state update that moves it toward a stopping condition within the allowed domain.
-
Filtering Values While Iterating
A filter retains the original items satisfying a predicate, typically in the order they were visited.
-
Transforming Each Item in a Loop
A mapping loop applies one transformation per input item. The result typically keeps positional correspondence with the source.
-
Reading List Comprehensions
A list comprehension builds a list by iterating, optionally selecting items, and evaluating the requested result expression for each selected item.
-
Mutating a List While Iterating
Changing a list's structure during traversal can move elements relative to the iterator and cause a step to skip an item.
-
Finding the First Matching Item
A first-match scan returns the earliest visited item satisfying a predicate and stops as soon as it has that witness.
-
Computing a Running Minimum
A running minimum stores the best value encountered at each point of a traversal and requires a valid empty-input policy.
-
Reducing a Sequence with Any or All
Existential and universal checks summarize a sequence of predicates and can finish before visiting every item.
-
Computing a Sequence of Prefix Totals
A prefix scan emits the accumulator after each input so later code can inspect cumulative work.
-
Maintaining a Running Average
A running average can keep a sum and an observation count rather than retaining every input.
-
Grouping Values by a Computed Key
To group values, compute one key per item and append the item to the collection for that key.
-
Bounded Retry Loops
A retry loop repeats an operation only for selected outcomes and needs a clear attempt budget and stopping rule.
-
Iterating Through Text Lines
A text file can provide one line at a time, which makes it possible to process input without retaining every record.
-
Flattening One Level of a Nested Sequence
One-level flattening concatenates the child sequences in traversal order and leaves values deeper in the structure as values.
-
Processing Consecutive Batches
Batching groups consecutive inputs under a size limit and must specify the treatment of the final incomplete batch.
-
Comparing Adjacent Items
Adjacent pairs describe transitions between neighboring values. Count them by looking at the gaps between items.
-
Stopping a Search Across Nested Loops
Stopping at the first matching pair requires leaving both loops or expressing the search as an operation with a result.
-
Exhaustion of a One-Pass Iterator
An iterator can consume its source as it advances. Reusing an exhausted iterator yields no new items under the normal iterator protocol.
-
Scanning from the Front or Back
Traversal direction determines which qualifying item a short-circuit search encounters first.
-
Finding Minimum and Maximum in One Pass
A single traversal can maintain both extremes, which is useful for inputs that can be visited only once.