Concepts / Working with Lists in Python

Working with Lists in Python

A for loop processes a list one item at a time.

  • Programming
Interactive lab

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

  1. Initialise the accumulator before the loop.
  2. Each iteration, the loop variable takes the next value from the list.
  3. An if inside the loop decides whether to update the accumulator.
  4. 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.

Educational simulation

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.

Key point

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.

python
Output
4
items[pen, book, lamp, cup]count0pencount = 1bookcount = 2lampcount = 3cupcount = 4initializeprocess itemincrementincrementincrement
How does the counter increase on each iteration, and how does each iteration correspond to one item in the list?

The 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.

Code walkthrough
values = [3, 41, 20, 12]
total = 0

for value in values:
    total += value

print(total)
  1. 1
    The list supplies one number to value during each iteration.
  2. 2
    On the first iteration, total changes from 0 to 3.
  3. 3
    On the second iteration, total changes from 3 to 44.
  4. 4
    On the third iteration, total changes from 44 to 64.
  5. 5
    On 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
Output
76
total03total = 341total = 4420total = 6412total = 76+ 3+ 41+ 20+ 12
How does the accumulator change after each list item is processed, and what value does it hold when the loop ends?

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

TaskInitial valueUpdate inside the loopUses current item value
Countingcount = 0Increase by 1No
Summingtotal = 0Add the current itemYes

Both patterns initialize before the loop and update once per iteration, but their updates answer different questions.

Counting loopinitialize and incrementSumming loopinitialize and addlen(items)returns item countsum(values)returns numeric totalsame countsame total
How does a manual counting or summing loop compare with the corresponding built-in function?

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.

python
Output
4
76

Mistakes That Break Accumulators

  • Common mistake

    Initializing the accumulator inside the loop

    What it looks like
    for value in values:
        total = 0
        total += value
    Why it fails

    The accumulator is reset on every iteration, so it cannot preserve the total from earlier items.

    Do this instead

    Set total = 0 before the loop, then update total inside the loop.

  • Common mistake

    Adding 1 when the task is summing

    What it looks like
    for value in values:
        total += 1
    Why it fails

    This counts iterations rather than adding the actual values.

    Do this instead

    Use total += value when computing the sum.

  • Common mistake

    Adding the current value when the task is counting

    What it looks like
    for item in items:
        count += item
    Why it fails

    A count should increase by one for each item regardless of the item's value.

    Do this instead

    Use count += 1 for a counting loop.

Best practice

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

MEDIUM

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?

  • 5, 15, 17

  • 1, 2, 3

  • 0, 5, 15

  • 5, 10, 2

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

  1. 1
    A counting loop initializes a counter before the loop and increases it by 1 for every item.
  2. 2
    A summing loop initializes a total before the loop and adds the current item's value on every iteration.
  3. 3
    The accumulator must not be reset inside the loop.
  4. 4
    Tracing the accumulator after each iteration helps verify that the loop is correct.
  5. 5
    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.

Related concepts

More concepts starting with W