Understanding List Structure and Indexing
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, you often need to process each item in turn. A for loop provides a natural way to visit every element without manually accessing each element by its index. The loop moves through the list in order, and its loop variable holds the actual element value for the current iteration.
What do you think happens?
Suppose a list contains the values red, green, and blue. What values will the loop variable receive as the loop visits the list?
Reveal answer
The values red, green, and blue
During direct list iteration, the loop variable holds the actual element value on each iteration, not the element's index.
Tracing List Traversal
A for loop traverses a list by repeatedly taking the next element and assigning that element to the loop variable. For a list with three elements, there are three iterations: the first iteration receives the first element, the second receives the second element, and the third receives the third element. The loop body runs once for each element.
Following Three Iterations
A list contains the elements apple, pear, and plum. Predict the value held by the loop variable during each iteration.
- 1First iteration
The loop variable receives apple, the first element.
- 2Second iteration
The loop variable receives pear, the next element.
- 3Third iteration
The loop variable receives plum, the final element.
The loop variable receives apple, then pear, then plum. The loop body runs three times.
Values Versus Positions
In direct iteration over a list, the loop variable represents the actual element value, not the element's index. If a program needs the index, the source explains that it should use range(len(list)) instead. This creates an important distinction: direct iteration focuses on what each element is, while index-based iteration is used when the position is needed.
| Iteration focus | What the loop variable represents | When it is useful |
|---|---|---|
| Direct list iteration | The actual element value | When you want to process each item |
| Index-based iteration | An index used to identify a position | When you need the list position; use range(len(list)) |
The central difference between visiting values directly and working through positions.
Nested and Empty Lists
A nested list is still one element of its containing list. If an outer list contains ordinary values and another list, one iteration visits the nested list as a whole. The loop does not automatically visit the values inside that nested list individually during that single traversal.
Counting Outer Iterations
An outer list contains alpha, a nested list containing beta and gamma, and delta. Predict the values received during one traversal of the outer list.
- 1First iteration
The loop receives alpha.
- 2Second iteration
The loop receives the entire nested list containing beta and gamma as one element.
- 3Third iteration
The loop receives delta.
The outer traversal has three iterations, not four. Beta and gamma remain inside the nested-list element during this traversal.
An empty list contains no elements, so the loop body executes zero times. This is normal behavior, not an error. To predict the result, first determine how many elements the list contains: a nonempty list gives one iteration per element, while an empty list gives no iterations.
Mistakes in Prediction
Common mistake Assuming the loop variable stores an index
What it looks likeTreating the loop variable as position 0, position 1, and position 2 during direct list iteration.
Why it failsThe loop variable holds the actual element value, not the index.
Do this insteadPredict the values in the list. If the index is required, use range(len(list)) instead.
Common mistake Counting the values inside a nested list as separate outer iterations
What it looks likeExpecting an outer list containing alpha, a nested list of beta and gamma, and delta to produce four outer iterations.
Why it failsThe nested list is counted as one element of the outer list.
Do this insteadCount the nested list as one outer element during that traversal.
Common mistake Treating an empty list as an error
What it looks likeExpecting the loop body to run once even though the list has no elements.
Why it failsThere is no element to assign to the loop variable.
Do this insteadExpect zero loop-body executions for an empty list.
Common mistake Predicting output without counting elements first
What it looks likeReading the loop body before identifying how many elements the list contains.
Why it failsThe number of iterations depends on the elements available in the list.
Do this insteadCount the outer list's elements, then write down the value received at each iteration in order.
Prediction Practice
Consider an outer list with these three elements: north, a nested list containing east and west, and south. Before running a for loop over the outer list, write down the value received on each iteration and the total number of iterations. Then consider an empty list and predict how many times its loop body executes.
Hints
- Count the nested list as one element of the outer list.
- The loop variable receives element values rather than indexes.
- An empty list has no element available for an iteration.
What do you think happens?
For the outer list containing north, a nested list containing east and west, and south, what are the three values received during the outer traversal?
Reveal answer
The values are north, the entire nested list containing east and west, and south.
The nested list is one element of the outer list, so the outer loop has three iterations. An additional traversal would be needed to visit the nested values individually.
Key Takeaways
- 1A for loop visits list elements in order, once per element.
- 2The loop variable holds the actual element value, not its index.
- 3Use
range(len(list))when the index is needed. - 4A nested list is one element during traversal of its containing list.
- 5An empty list causes the loop body to execute zero times.
At a glance
- Direct list iteration gives the loop variable each actual element value in order.
- The number of loop iterations matches the number of elements in the list being traversed.
- A nested list counts as one element of its outer list.
- An empty list makes the loop body run zero times.
- Predict the element sequence before running the loop, and distinguish values from indexes.