Using Built-in Functions with Lists
The running total method maintains only a sum and count, discarding individual numbers after computing the average.
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.
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 processed | total | count | Individual values retained? |
|---|---|---|---|
| 4 | 4 | 1 | No |
| 7 | 11 | 2 | No |
| 9 | 20 | 3 | No |
Generated trace showing the state after each number is processed by the running-total method.
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.
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.
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.
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
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.
| Situation | Best approach | Reason |
|---|---|---|
| Only one final average is needed | Running total | Only total and count are needed. |
| Maximum and minimum are needed later | List storage | The original values remain available. |
| Every entered value must be inspected during debugging | List storage | The complete input collection is retained. |
Practice review based on the information each task needs to retain.
Key Takeaways
- The running-total method keeps only a total and a count while numbers are processed.
- The list method stores every number and uses sum() for the total and len() for the count.
- Both methods can compute the same average, but list storage preserves the original data.
- Use a running total for a simple one-result calculation and list storage for later analysis, comparison, or debugging.
- 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.