Using Built-in Functions with Lists
The list method is often the better choice for learning and for programs that may need more than one calculation, because the original data remains available.
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.
- 1Inspect the list
The list retains all three original numbers: 4, 7, and 9.
- 2Apply sum()
sum()adds the list values, giving a total of 20. - 3Apply len()
len()counts the list elements, giving a count of 3. - 4Combine 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
Common mistake Starting with a list that has not been initialized
What it looks likeTrying to append numbers before creating the empty list
Why it failsThe list approach requires an empty list before the input loop begins.
Do this insteadCreate the empty list before processing the first number.
Common mistake Keeping input as text instead of converting it to numbers
What it looks likeAdding an input string directly to a numeric total or storing it as a nonnumeric value
Why it failsThe average calculation needs numbers that can be added and divided.
Do this insteadConvert each user input to a number before adding it to the total or appending it to the list.
Common mistake Dividing by the length of an empty list
What it looks likeCalculating the average when no numbers were entered
Why it failsThere is no count to divide by when the list is empty.
Do this insteadCheck that the list is not empty before dividing by its length.
Common mistake Assuming the running-total method can recover every original value
What it looks likeTrying to find a median after retaining only total and count
Why it failsThe running-total method discards individual numbers.
Do this insteadUse 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
- 1The running-total method keeps only a total and a count while numbers are processed.
- 2The list method stores every number and uses
sum()for the total andlen()for the count. - 3Both methods can compute the same average, but list storage preserves the original data.
- 4Use a running total for a simple one-result calculation and list storage for later analysis, comparison, or debugging.
- 5Initialize the list before the loop, convert inputs to numbers, and check that the collection is not empty before dividing.
At a glance
- Running totals retain a sum and count but discard individual values.
- Lists retain every entered value, making further analysis and debugging possible.
sum()andlen()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.