Concepts / The append() Method and List Mutation

The append() Method and List Mutation

The running total method maintains only a sum and count, discarding individual numbers after computing the average.

  • Programming

Two Ways to Keep Input Data

When a program computes the average of numbers entered by a user, it must keep track of the data while the loop runs. One approach keeps only a running total and a count. Another approach stores every number in a list. Both approaches can produce the same average, but they preserve different amounts of information. The running-total approach is suitable when the final average is the only result needed. The list approach is more flexible because the original values remain available after the loop.

The central design question is not only how to calculate the average. It is also what information the program must still have after each input has been processed.

Appending Changes the List

append() adds one value to the end of an existing list. The important idea is mutation: the list itself changes as each value is appended. Before the loop, the list is empty. After the first input, it contains one value. After the next input, it contains both values, and so on. Because earlier values remain in the list, the program can inspect the complete collection after the loop ends.

append 10append 20append 30Empty list[]First value[10]Second value[10, 20]Third value[10, 20, 30]
What does the list contain after each number is appended, and how does its state change over time?

The values in this visual are a generated example. The general pattern is the important part: each append operation preserves the values already collected and adds one more value at the end.

Running Total or Stored List

After an input valueRunning-total methodList-storage method
Information retainedThe sum so far and the number of values so farEvery value entered so far
State for the generated values 10, 20, 30Total and count progress to 60 and 3The list progresses from [10] to [10, 20] to [10, 20, 30]
After the average is computedThe individual values are not retainedThe individual values remain available
Best fitOne final result with lower memory useFurther analysis, comparisons, or debugging
contributes tocontributes tocontributes tostored asstored asstored as10input valueTotal and count60 and 320input valueNumber list[10, 20, 30]30input value
After each input number, what information is retained by the running-total method compared with the list-storage method?

The running-total method compresses the inputs into two pieces of information: their sum and their count. That is enough for an average, but it is not enough to recover the original values. The list method keeps the complete data set. This difference affects what the program can do later.

Analyzing the Collected List

Average from stored values

A program collects the generated values 10, 20, and 30 in a list. How does it use sum() and len() to compute the average?

Collect: Start with an empty list and append each value. After the three inputs, the list contains 10, 20, and 30.

Add: Call sum() with the list. It processes the list's elements and returns their total, which is 60 for this generated example.

Count: Call len() with the list. It returns the number of elements, which is 3 for this generated example.

Divide: Divide the total by the count. The resulting average is 20.

The list remains available after the calculation, while sum() and len() provide the total and count needed for the average.

sum() takes a list of numbers and returns their total. len() takes a sequence, including a list, and returns the number of elements in it. Using these built-in functions means the program does not need to write an additional explicit loop just to add the values or count them. Together, sum() and len() make the list-storage approach concise while preserving the original data.

The average calculation uses the collected list as its source: sum(list) supplies the total, len(list) supplies the count, and the list itself remains available for later work.

Tracing Both Methods

After processingRunning totalCountStored listAverage from processed values
10101[10]10
20302[10, 20]15
30603[10, 20, 30]20

Generated trace showing the state after each value is processed.

A trace makes the different state models visible. In the running-total method, the total and count change on every iteration. In the list method, the list grows on every iteration. The average shown in the final column can be computed after each row for tracing purposes, although the source description emphasizes computing it after the input loop ends.

Choosing the Retention Level

Choose the running-total method when the program needs one final result, such as an average, and does not need the original values afterward. It uses less memory and is simpler for that narrow task. Choose list storage when the program may need further analysis, comparisons, or debugging. Keeping the values makes it possible to find a median, identify outliers, or find a maximum or minimum. These tasks require access to the original data rather than only its sum and count.

NeedPrefer running totalPrefer list storage
Only one final averageYesNot necessary
Lower memory useYesNo; values are retained
Debug the original inputsNoYes
Compute several statistics from the same dataLimitedYes

Mistakes That Lose Data

  • Failing to initialize the list before the loop

    The list must exist before values can be added to it.

    Fix: Create the empty list before the input loop begins.

  • Expecting a running total to preserve the original numbers

    The running-total method keeps only the sum and count, not the individual values.

    Fix: Use list storage when later analysis needs the original data.

  • Dividing by the length of an empty list

    There is no collected data to average, and the source guidance requires checking that the list is not empty before dividing by its length.

    Fix: Check that the list contains data before performing the division.

  • Forgetting that append() changes the existing list

    append() adds to the list; it does not replace the values already present.

    Fix: Trace the complete list after each append.

Practice the State Trace

EASY

A program collects the generated values 4, 8, and 12. Trace both approaches after each value. For the running-total approach, record the total and count. For the list approach, record the complete list. Then determine the final average and state which original values remain available after the calculation.

Hints
  • Increase the running total by the newest value.
  • Increase the count by one for each processed value.
  • For list storage, keep all earlier values and append the newest value.
  • Use the final total divided by the final count, or use sum() divided by len() on the final list.

What do you think happens?

After processing 4, 8, and 12 with list storage, what information is still available?

  • Only the total and count
  • Only the final value
  • All three original values, along with the list length and total when calculated
  • No information after the average is computed
Reveal answer

Answer: All three original values, along with the list length and total when calculated

The list method preserves every value that was appended. sum() and len() can analyze the list without removing its elements.

Key Takeaways

  1. The running-total method keeps a sum and a count, then uses them to compute an average.
  2. The list method appends every input value, preserving the original data after the loop.
  3. sum() returns the total of the list elements, while len() returns the number of elements.
  4. Use a running total for a single final result when lower memory use matters; use a list when later analysis or debugging requires the original values.
  5. Initialize the list before the loop, convert inputs to numbers, and check that the list is not empty before dividing by its length.

Key Takeaways

  • append() mutates a list by adding a value to its end while preserving values already collected.
  • A running total retains only the sum and count, whereas list storage retains every input value.
  • sum() and len() provide the total and count needed to calculate an average from a list.
  • List storage is preferable when the program must debug the inputs or perform additional analysis after collection.