Understanding List Basics
List traversal comes in two main forms: simple for loops for reading elements, and index-based loops using range(len()) for updating elements.
Two Ways to Move Through a List
When a loop moves through a list, the right pattern depends on what you need to do. A simple for loop gives you each element's value directly, which is useful when you only need to inspect or display the elements. An index-based loop gives you each position, allowing you to read an element and update it in place.
Reading Values Directly
In a simple for loop, the loop variable receives each list element's value in turn. The loop moves through the list in element order, so the first iteration uses the first value, the next iteration uses the next value, and so on. This pattern is appropriate when the task is to inspect or display values rather than change the list.
red
green
blueThe loop variable in a simple for loop represents the current value, not the position of that value. This makes the pattern convenient for reading, but it does not provide the position needed to replace an element in the list.
Generating Positions for Updates
When a list element must be changed, the loop needs access to that element's position. The pattern for i in range(len(list)) combines len() and range() to visit the positions of the list. The loop variable i represents the current index, and the list can be accessed through that index for reading or modification.
Here, i identifies a position rather than directly holding the element's value. The expression scores[i] reads the current element, and assigning a new value to scores[i] updates that position in the list.
Tracing an In-Place Update
What do you think happens?
What list results after this loop finishes?
Reveal answer
Answer: [4, 7, 10]
The loop visits each position. At index 0, 3 becomes 4; at index 1, 6 becomes 7; at index 2, 9 becomes 10.
Following Each Index
Start with values = [3, 6, 9]. For each index in range(len(values)), add 1 to the element at that index.
Index 0: The element at position 0 is 3. Replacing it with 3 + 1 changes the list to [4, 6, 9].
Index 1: The element at position 1 is 6. Replacing it with 6 + 1 changes the list to [4, 7, 9].
Index 2: The element at position 2 is 9. Replacing it with 9 + 1 changes the list to [4, 7, 10].
The final list is [4, 7, 10].
During an updating traversal, the loop changes the list one position at a time. Earlier changes remain in the list while later indices are processed. To predict the final list, record the index visited, the value originally at that position, and the replacement value.
Mistakes in Traversal Patterns
Using a simple value loop when the task requires changing list elements
The loop variable receives a value directly, but this pattern does not use a list position to replace the corresponding element.
Fix:
Use for i in range(len(scores)) and assign the replacement through scores[i].Confusing the index with the element value
This prints the generated positions rather than the values stored at those positions.
Fix:
Use scores[i] when you need the element associated with the current index.Updating the wrong position while tracing the loop
Each iteration acts on the position held by the current index.
Fix:
Track the index in order and update only the corresponding list position.
Practice the Trace
Trace the loop without running it. Write down the list after each index is processed, then give the final list. values = [2, 5, 8] for i in range(len(values)): values[i] = values[i] * 2
Hints
- The index-based loop visits each position in order.
- At each position, replace the current value with that value multiplied by 2.
- Keep the changes from earlier iterations when processing the next index.
Practice Check
Determine the final list for values = [2, 5, 8] when each element is replaced by twice its value.
Index 0: The value 2 becomes 4, giving [4, 5, 8].
Index 1: The value 5 becomes 10, giving [4, 10, 8].
Index 2: The value 8 becomes 16, giving [4, 10, 16].
The final list is [4, 10, 16].
Traversal Checklist
- A simple for loop visits list values directly and is suited to inspecting or displaying elements.
- An index-based loop visits positions generated by range(len(list)).
- Use the index expression, such as list[i], when you need to replace an element in the list.
- To trace an update, follow each index in order and record the list after every replacement.
- The final list contains all changes made at the positions visited by the index-based loop.
Key Takeaways
- Simple for loops provide list values directly for reading.
- Index-based loops using range(len(list)) provide positions for reading and updating.
- The expression list[i] connects the current index to the element that can be changed.
- Tracing each index and intermediate list state makes final mutations predictable.