Iterating Through Lists with Loops
The central idea is simple: a for loop takes the items of a list in sequence and executes its body once for each item.
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…
One List, Many Actions
Suppose you have a list of words, numbers, or other items and want to perform the same action for every item. A for loop provides a direct way to move through that known collection. Instead of describing a separate action for each item, you describe one loop body, and that body runs once for each item in the list.
The central idea is simple: a for loop takes the items of a list in sequence and executes its body once for each item.
The List as an Ordered Sequence
A list is also a sequence. Its items are arranged as an ordered collection, so iteration visits one item after another rather than treating the list as an unstructured group. The loop continues through the known items until it has processed the list.
For example, a list of words can be processed word by word, and a list of numbers can be processed number by number. The same loop structure applies because both are lists of items to visit.
The Loop's Progression
At the beginning of a for loop, the list supplies the items that will be visited. The loop selects the first item and runs its body. It then moves to the next item and runs the same body again. This continues until every item in the list has been processed.
Tracing Three Items
Imagine a list containing three words. Trace how a for loop processes the list.
- 1First iteration
The loop takes the first word and executes the loop body once for that word.
- 2Second iteration
The loop moves to the second word and executes the same body once more.
- 3Third iteration
The loop takes the third word and executes the body for the final item.
- 4Completion
There are no more items in the known list, so the loop stops.
The body runs three times, once for each word in the list.
What do you think happens?
If a list contains three items, how many times does the body of a for loop run when it processes that list?
Reveal answer
Three times
A for loop processes the known set of items, so its body runs once for each of the three items.
Definite and Indefinite Repetition
| Loop type | What controls repetition | Relationship to a list |
|---|---|---|
| for loop | A known set of items | Runs once for each item in the set |
| while loop | A condition | Continues until the condition becomes False |
The source distinguishes the for loop as definite repetition and the while statement as indefinite repetition.
A for loop is called definite because the loop is working through a known set of items. If the list has three items, the loop has three item visits to carry out. A while loop is different in this respect: it keeps looping until its condition becomes False. The important distinction is what determines the repetition: the items in the known set for a for loop, and a condition for a while loop.
Scanning for a Useful Value
Iteration is useful when the goal is not merely to visit items but to inspect them for a result. A loop can go through a list of values while looking for something such as the largest or smallest value. The repeated scan gives the program an opportunity to consider each item in the collection.
Finding an Extreme Value
A list contains several numbers, and the task is to look through the list for the largest value.
- 1Visit the first number
The loop begins scanning the list by processing its first item.
- 2Visit each following number
The loop continues through the remaining items, considering each one in sequence.
- 3Compare the scanned values
The process is looking for the largest value among the data that has been scanned.
- 4Finish the scan
After the final item has been processed, the loop has examined the known list.
A for loop can scan every item in a list when searching for the largest or smallest value.
The loop does not need a different body for each number. The same body is applied as the loop moves from one list item to the next.
Mistakes in Loop Tracing
Common mistake Treating the list as an unordered group
What it looks likeReasoning that the loop can visit the items in any order.
Why it failsA list is a sequence, and iteration moves through its items in sequence.
Do this insteadTrace the first item, then the next item, continuing until the final item.
Common mistake Assuming the loop body runs only once
What it looks likeSeeing one loop body and expecting one total execution.
Why it failsThe body executes once for each item in the list.
Do this insteadCount the items in the known set and use that count to trace the iterations.
Common mistake Describing a for loop as condition-controlled repetition
What it looks likeSaying that a for loop continues until a condition becomes False.
Why it failsThat description belongs to the source's explanation of a while statement. A for loop goes through a known set of items.
Do this insteadDescribe a for loop as definite repetition over the items in a known set.
Common mistake Stopping before the final item
What it looks likeProcessing the first two items of a three-item list and declaring the loop finished.
Why it failsThe loop runs through as many iterations as there are items in the set.
Do this insteadContinue tracing until every item has been visited.
The reliable stopping rule for this topic is collection-based: once the for loop has processed every item in the known list, its list iteration is complete. Do not replace that rule with the while-loop rule about waiting for a condition to become False.
Practice the Trace
A list contains five items. Without writing code, describe the order in which a for loop visits them and state how many times the loop body runs.
Hints
- A list is a sequence, so begin with the first item.
- The loop moves to the next item after each body execution.
- The number of item-based iterations matches the number of items in the known set.
A list of numbers is being scanned to find the smallest value. Explain why the loop must process every item before the scan is complete.
Hints
- The loop is looking for a value among the data being scanned.
- The list supplies a known set of items to visit.
- The final item marks completion of the list scan.
The Iteration Pattern
- 1A list is a sequence of ordered items.
- 2A for loop visits the list items in sequence.
- 3The loop body runs once for each item in the known set.
- 4A for loop is definite because its repetition is based on a known set of items.
- 5A loop can scan list data when searching for values such as the largest or smallest item.
Key Takeaways
- A for loop iterates through the items of a list in sequence.
- Its body runs once for every item in the known list.
- The loop stops after the final item has been processed.
- This definite behavior differs from a while loop, which continues until its condition becomes False.
- Iteration can be used to scan list data for values such as the largest or smallest item.