Nested Data Structures and Multi-Level 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 a list contains several items, a for loop provides a direct way to visit each item in turn. The loop variable receives the actual element value during each iteration. It does not automatically receive the element's index.
red
green
blueReading the Outer List
A list traversal visits the list's elements in order. On the first iteration, the loop variable contains the first element; on the next iteration, it contains the next element; and so on until every element has been visited. You usually do not need to access each element manually by index. If an index is specifically required, the source notes that you can use range(len(list)) instead.
Tracing Three Iterations
Determine the values assigned to item while traversing ["A", "B", "C"].
First iteration: item contains the first list element, "A".
Second iteration: item contains the second list element, "B".
Third iteration: item contains the third list element, "C".
The loop processes "A", then "B", then "C".
Nested Lists as Outer Elements
A nested list is still one element from the point of view of the outer list. If an outer list contains values and another list, the outer loop does not automatically visit the individual values inside that nested list. Instead, one iteration assigns the entire inner list to the loop variable.
Empty List Behavior
An empty list contains no elements to visit. Therefore, a for loop over an empty list performs zero iterations: the loop body never executes. This is expected behavior, not an error.
finishedPredicting the Output
What do you think happens?
What values are printed, and in what order, when the loop visits ["first", "second", "third"]?
Reveal answer
Answer: first, second, third
The loop variable receives each actual element value in list order. It does not automatically receive the indexes.
Predict the output before running this loop: values = ["north", "south"]. The loop prints each value as it visits the list.
Hints
- The loop variable receives actual values rather than indexes.
- Follow the list from its first element to its last element.
Mistakes with Loop Levels
Assuming the loop variable contains an index
A standard for loop over a list assigns the actual element value to the loop variable.
Fix:
Reason about the values 10, 20, and 30. Use range(len(list)) when the index is needed.Assuming an outer loop automatically reaches inner values
Each inner list is one element of the outer list.
Fix:
Use a second loop when the individual elements inside each nested list must be visited.Treating zero iterations as an error
An empty list has no elements, so there is nothing for the loop to assign to its variable.
Fix:
Expect the loop body to execute zero times and continue with the code after the loop.
Before running a loop, write down the outer list's elements in order. For a nested list, keep each inner list grouped as one outer element. Only split that inner group into individual values when a second loop processes it.
Practice Traversal
For each case, predict how many times the loop body executes and what the loop variable receives: a list containing four ordinary values; an empty list; and a list containing two nested lists. Then explain what additional loop is needed to reach values inside the nested lists.
Hints
- Count the elements of the list being directly traversed.
- An empty list has no element for the loop variable.
- Treat each nested list as one element until a second loop traverses it.
Essential Takeaways
- A for loop visits list elements in order.
- The loop variable contains the actual element value, not the index.
- An empty list causes zero loop-body executions and is not an error.
- A nested list is one element of its outer list during outer-loop iteration.
- A second loop is needed to visit the individual elements inside each nested list.
Key Takeaways
- A for loop traverses a list by assigning each actual element to its loop variable in order.
- The loop variable is a value, not an index.
- An empty list produces zero iterations.
- A nested list remains one outer element until another loop traverses its contents.
- Predict output by tracing the elements at the current loop level.