For Loops and String Iteration
For loops are the most common and natural way to iterate through list elements, using identical syntax to string iteration.
One Element at a Time
When you have a list of items, you often need to do something with each item in turn. A for loop provides a clean way to visit every element without manually accessing each element by its index. The loop moves through the list in order, and the loop variable receives the actual element value during each iteration.
What do you think happens?
Suppose a loop visits the list ["red", "green", "blue"]. What value does the loop variable receive first, and what values follow?
Reveal answer
Answer: The loop variable receives "red", then "green", then "blue".
A for loop over a list visits the actual elements. It does not place the element indexes in the loop variable.
The Traversal Sequence
items = ["red", "green", "blue"] for item in items: print(item)
The name item is a loop variable. During one iteration it contains one actual list element. On the next iteration, it contains the next element. It is not an index such as 0, 1, or 2.
Reading the Loop Variable
Predicting Each Iteration
A loop traverses the list [10, 20, 30]. What does the loop variable contain on each pass?
First pass: The loop variable contains the first element, 10.
Second pass: The loop variable contains the next element, 20.
Third pass: The loop variable contains the final element, 30.
Index check: The loop variable contains 10, 20, and 30, not the indexes 0, 1, and 2.
The loop processes the values 10, 20, and 30 in that order.
Nested Lists Stay Whole
A nested list is still one element of its outer list. When a for loop traverses the outer list, it does not automatically enter the inner lists. Each inner list is delivered to the loop variable as one complete value during one iteration.
['a', 'b']
['c', 'd']The outer loop has two iterations in this example because the outer list has two elements. Those elements happen to be lists, but each inner list is treated as one element by the outer loop.
Empty List Behavior
An empty list contains no elements to visit. Therefore, a for loop over an empty list executes its loop body zero times. Nothing inside the body is processed, and this is expected behavior rather than an error.
doneStrings Use the Same Pattern
The syntax for iterating over a list is identical to the syntax used for iterating over a string. In both cases, the for loop assigns the next element of the sequence to the loop variable and processes that value before moving on.
| Collection | Value received by the loop variable | Traversal idea |
|---|---|---|
| List | Each list element | Visit the list elements in order |
| String | Each character | Visit the string characters in order |
Mistakes in Prediction
Assuming the loop variable contains an index
A direct for loop over a list assigns actual element values to the loop variable, not element indexes.
Fix:
Predict the element values first. Use range(len(list)) when the task specifically requires indexes.Expecting a nested list to be unpacked automatically
Nested lists are treated as single elements during iteration of the outer list.
Fix:
Count the elements of the outer list first. An inner list contributes one outer iteration.Treating an empty-list loop as an error
There are no elements available for the loop to process, so no iteration is expected.
Fix:
Predict no loop-body output and continue reasoning about the statements after the loop.
Prediction Practice
Before running the loop, write down the value of item on every iteration and the order of the printed output. items = ["first", ["inside", "list"], "last"] for item in items: print(item)
Hints
- Count the elements in the outer list.
- Treat the nested list as one outer element.
- The loop variable receives values, not indexes.
Checking the Prediction
Determine the loop-variable values for ["first", ["inside", "list"], "last"].
First iteration: The loop variable receives the string "first".
Second iteration: The loop variable receives the nested list ["inside", "list"] as one complete element.
Third iteration: The loop variable receives the string "last".
The loop processes three outer elements in order: "first", ["inside", "list"], and "last".
Key Takeaways
- A for loop is a natural way to visit every element of a list in order.
- The loop variable holds the actual element value, not its index.
- An empty list causes zero loop-body executions and is not an error.
- A nested list is treated as one element when iterating through its outer list.
- List and string iteration use the same for-loop syntax pattern.
Key Takeaways
- For loops traverse list elements one at a time and in order.
- The loop variable receives each actual element rather than an index.
- Nested lists count as single elements during iteration of the outer list.
- An empty list produces no loop-body iterations.
- String iteration follows the same basic pattern as list iteration.