Working with Lists in Python
A for loop processes a list one item at a time.
Try it: Loop Tracer
How a Python for loop visits each item in turn, and how an accumulator variable (a total, a count, a best-so-far or a result list) changes on every iteration.
How it works
- Initialise the accumulator before the loop.
- Each iteration, the loop variable takes the next value from the list.
- An if inside the loop decides whether to update the accumulator.
- After the last item the loop ends and the accumulator holds the answer.
Default run (13 steps): values = [4, 9, 2, 7, 5]. Run the loop one statement at a time. … The loop has used every value. print(total) shows 27.
Simplified: A model of five fixed loop programs (sum, count, maximum, minimum, filter) run on your list — it steps the program exactly as Python would, but it does not run Python.
Loading the simulation…
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)- 1The list supplies one number to value during each iteration.
- 2On the first iteration, total changes from 0 to 3.
- 3On the second iteration, total changes from 3 to 44.
- 4On the third iteration, total changes from 44 to 64.
- 5On the fourth iteration, total changes from 64 to 76.
- total starts at 0 before the loop
- value is the current list item
- total increases by the current value on each iteration
- the final total is printed after the loop
76At 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
Common mistake Initializing the accumulator inside the loop
What it looks likefor value in values: total = 0 total += valueWhy it failsThe accumulator is reset on every iteration, so it cannot preserve the total from earlier items.
Do this insteadSet total = 0 before the loop, then update total inside the loop.
Common mistake Adding 1 when the task is summing
What it looks likefor value in values: total += 1Why it failsThis counts iterations rather than adding the actual values.
Do this insteadUse total += value when computing the sum.
Common mistake Adding the current value when the task is counting
What it looks likefor item in items: count += itemWhy it failsA count should increase by one for each item regardless of the item's value.
Do this insteadUse 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
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
- 1A counting loop initializes a counter before the loop and increases it by 1 for every item.
- 2A summing loop initializes a total before the loop and adds the current item's value on every iteration.
- 3The accumulator must not be reset inside the loop.
- 4Tracing the accumulator after each iteration helps verify that the loop is correct.
- 5Use
len()for basic list counts andsum()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()andsum()for basic counting and summing in practical Python code.