Concepts / Using Built-in Functions with Lists

Using Built-in Functions with Lists

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

  • Programming

One Average, Two Data Strategies

When a program receives numbers from a user, it can compute an average in two different ways. The running-total method keeps a total and a count while the input arrives. The list method stores every number and analyzes the complete collection after the loop. Both methods can produce the same average, but they preserve different information.

add and countdivideappendsum and countInput numbersone number at a timeRunning totaltotal and countAveragetotal divided by countNumber listevery entered valueAveragesum(list) divided bylen(list)
What information is retained or discarded by each approach as new numbers arrive, and how do both compute the average?

Tracing the Running Total

The running-total method begins with a total and a count. Each entered number is added to total, and count increases by one. After all numbers have been processed, the average is calculated by dividing total by count. The individual numbers are not retained by this method.

Number processedtotalcountIndividual values retained?
441No
7112No
9203No

Generated trace showing the state after each number is processed by the running-total method.

add 4, add 1add 7, add 1add 9, add 1divide total by countStarttotal 0, count 04 processedtotal 4, count 17 processedtotal 11, count 29 processedtotal 20, count 3Average20 divided by 3
How do total and count change after each number is processed?

Keeping the Complete List

The list approach starts with an empty list. Each entered number is appended to that list. Once the loop ends, sum() adds the numbers in the list and len() counts its elements. Dividing the result of sum() by the result of len() produces the average.

containscontainscontainstotal20numlist[4, 7, 9]4original valuecount37original value9original value
What does memory contain after several inputs when the program stores only sum and count versus every individual number?

Average from a Stored List

Suppose the collected list contains 4, 7, and 9. Use the list method to determine the average.

Inspect the list: The list retains all three original numbers: 4, 7, and 9.

Apply sum(): sum() adds the list values, giving a total of 20.

Apply len(): len() counts the list elements, giving a count of 3.

Combine the results: Divide the total, 20, by the count, 3, to compute the average.

The average is 20 divided by 3. The complete list remains available for later analysis.

numberselementstotalcountnumlistcollected numberssum()totalAveragesum result divided by lenresultlen()element count
How does data move from the list into sum() and len(), and how are their results combined?

Choosing What to Retain

Choose the running-total method when the program needs one final result and does not need the original values afterward. It is simpler, uses less memory, and runs slightly faster. Choose the list method when the values may be needed for further analysis, comparisons, or debugging. Keeping the list makes it possible to examine the original data for tasks such as finding a median, identifying outliers, or finding a maximum or minimum.

analyzeanalyzecomputeComplete listall original valuesMedianuses original dataAveragefinal resultOutliersuses original dataTotal and countsummary only
When does keeping the complete list enable analyses or debugging that a running total and count cannot support?

Mistakes That Change the Result

  • Starting with a list that has not been initialized

    The list approach requires an empty list before the input loop begins.

    Fix: Create the empty list before processing the first number.

  • Keeping input as text instead of converting it to numbers

    The average calculation needs numbers that can be added and divided.

    Fix: Convert each user input to a number before adding it to the total or appending it to the list.

  • Dividing by the length of an empty list

    There is no count to divide by when the list is empty.

    Fix: Check that the list is not empty before dividing by its length.

  • Assuming the running-total method can recover every original value

    The running-total method discards individual numbers.

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

Apply the Two Methods

MEDIUM

A program receives several user-entered numbers. Decide which approach is more appropriate in each situation: running total or list storage. First, the program only needs to display one final average. Second, the program must later identify the maximum and minimum values. Third, the program is being debugged and you want to inspect every value that was entered.

Hints
  • Ask whether the original individual values are needed after the average is computed.
  • Maximum, minimum, and debugging require access to the original data.
  • A single final average can be computed from total and count.
SituationBest approachReason
Only one final average is neededRunning totalOnly total and count are needed.
Maximum and minimum are needed laterList storageThe original values remain available.
Every entered value must be inspected during debuggingList storageThe complete input collection is retained.

Practice review based on the information each task needs to retain.

Key Takeaways

  1. The running-total method keeps only a total and a count while numbers are processed.
  2. The list method stores every number and uses sum() for the total and len() for the count.
  3. Both methods can compute the same average, but list storage preserves the original data.
  4. Use a running total for a simple one-result calculation and list storage for later analysis, comparison, or debugging.
  5. Initialize the list before the loop, convert inputs to numbers, and check that the collection is not empty before dividing.

Key Takeaways

  • Running totals retain a sum and count but discard individual values.
  • Lists retain every entered value, making further analysis and debugging possible.
  • sum() and len() provide the total and element count needed for a list-based average.
  • The right approach depends on whether the program needs only one result or needs the original data later.
  • A safe list-based average requires initialization, numeric inputs, and a nonempty list.