Working with Lists in Python
An accumulator is a variable that starts with an initial value before a loop, gets updated on each iteration, and holds the final result when the loop ends.
Tracking Across Iterations
A for loop processes a list one item at a time. Each iteration can use the current item, but many useful tasks also require remembering what happened in earlier iterations. For example, you may need to know how many items have been processed or what their running total is. An accumulator is a variable that starts with an initial value before the loop, is updated during every iteration, and holds the final result when the loop ends.
The essential accumulator pattern is initialize before the loop, update inside the loop, and use the final value after the loop.
Counting Items One by One
A counting loop answers the question: how many items are in this list? Initialize a counter to zero before the loop. Each time the loop processes an item, increase the counter by one. The current item's value does not matter because every item contributes exactly one to the count. The counter represents how many items have been seen so far.
4The iteration variable item controls which list element is processed, but its value is not used in this counting loop. The loop body runs once for each list item, so the counter increases once for each iteration. After the fourth iteration, count is 4.
Building a Running Total
A summing loop uses the same overall structure as a counting loop, but it updates the accumulator differently. Start total at zero before the loop. During each iteration, add the actual value of the current item to total. Unlike a counter, the summing loop must use the iteration variable because each item's value affects the result.
values = [3, 41, 20, 12] total = 0 for value in values: total += value print(total)
At every point in the loop, total means the sum of all items processed so far. It never resets inside the loop. Each new value depends on the previous total plus the current item. When the loop ends, total contains the sum of the complete list.
Counting Versus Summing
| Task | Initial value | Update inside the loop | Uses current item value |
|---|---|---|---|
| Counting | count = 0 | Increase by 1 | No |
| Summing | total = 0 | Add the current item | Yes |
Both patterns initialize before the loop and update once per iteration, but their updates answer different questions.
The counting and summing loops are valuable because they reveal the accumulator pattern. However, Python provides built-in functions for these two common tasks. len() returns the number of items in a list, and sum() returns the total of numeric items. For basic counting and summing, these functions are clearer, shorter, and more efficient than manual loops.
4
76Mistakes That Break Accumulators
Initializing the accumulator inside the loop
The accumulator is reset on every iteration, so it cannot preserve the total from earlier items.
Fix:
Set total = 0 before the loop, then update total inside the loop.Adding 1 when the task is summing
This counts iterations rather than adding the actual values.
Fix:
Use total += value when computing the sum.Adding the current value when the task is counting
A count should increase by one for each item regardless of the item's value.
Fix:
Use count += 1 for a counting loop.
When checking a summing loop, write down the accumulator after every iteration. If the value does not equal the previous accumulator plus the current item, the update is wrong. For a counting loop, check that the counter increases by exactly one for every processed item.
Practice the Pattern
Create a manual loop that counts the items in a list named readings. Then create a second loop that computes the total of a numeric list named scores. Before running either loop, predict the accumulator's value after each iteration.
Hints
- Initialize the counting variable to 0 before its loop.
- Increase the counter by 1 on every iteration.
- Initialize the summing variable to 0 before its loop.
- Add the current numeric item to the summing variable.
What do you think happens?
For values = [5, 10, 2], what is total after each iteration of a correct summing loop that starts with total = 0?
Reveal answer
Answer: 5, 15, 17
The loop adds the current item to the previous total: 0 + 5 = 5, then 5 + 10 = 15, then 15 + 2 = 17.
The Accumulator Checklist
- A counting loop initializes a counter before the loop and increases it by 1 for every item.
- A summing loop initializes a total before the loop and adds the current item's value on every iteration.
- The accumulator must not be reset inside the loop.
- Tracing the accumulator after each iteration helps verify that the loop is correct.
- Use len() for basic list counts and sum() for totals of numeric items.
Key Takeaways
- An accumulator preserves information across loop iterations.
- Counting uses a counter that increases by 1 for each item.
- Summing uses an accumulator that adds each current item value.
- Initialize before the loop, update inside the loop, and trace the state to check correctness.
- Prefer len() and sum() for basic counting and summing in practical Python code.