Iterating Through Lists with Loops
We have also used the for..in loop to iterate through the items of the list. By now, you must have realised that a list is also a sequence. The speciality of sequences will be discussed in a later section.
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.
First iteration: The loop takes the first word and executes the loop body once for that word.
Second iteration: The loop moves to the second word and executes the same body once more.
Third iteration: The loop takes the third word and executes the body for the final item.
Completion: 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
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 |
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.
Visit the first number: The loop begins scanning the list by processing its first item.
Visit each following number: The loop continues through the remaining items, considering each one in sequence.
Compare the scanned values: The process is looking for the largest value among the data that has been scanned.
Finish 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
Treating the list as an unordered group
A list is a sequence, and iteration moves through its items in sequence.
Fix:
Trace the first item, then the next item, continuing until the final item.Assuming the loop body runs only once
The body executes once for each item in the list.
Fix:
Count the items in the known set and use that count to trace the iterations.Describing a for loop as condition-controlled repetition
That description belongs to the source's explanation of a while statement. A for loop goes through a known set of items.
Fix:
Describe a for loop as definite repetition over the items in a known set.Stopping before the final item
The loop runs through as many iterations as there are items in the set.
Fix:
Continue 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
- A list is a sequence of ordered items.
- A for loop visits the list items in sequence.
- The loop body runs once for each item in the known set.
- A for loop is definite because its repetition is based on a known set of items.
- A 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.