List Methods: append(), remove(), and sort()
The four central functions are len(), max(), min(), and sum(). They analyze a list and return a result rather than changing the list itself.
Questions Lists Can Answer
When you work with a list, you often need to ask questions about its contents: How many items are present? What is the largest value? What is the smallest value? What is the total? Python provides built-in functions that answer these questions without requiring you to write a loop that manually visits every element.
The four central functions are len(), max(), min(), and sum(). They analyze a list and return a result rather than changing the list itself.
A First List Analysis
6
74
3
154
25.666666666666668The list contains six elements, so len(nums) returns 6. The largest element is 74, so max(nums) returns 74. The smallest element is 3, so min(nums) returns 3. Adding all six values produces 154, so sum(nums) returns 154. Dividing the total by the item count produces the average.
What Each Function Computes
| Function | Question answered | Data requirement | Result for nums |
|---|---|---|---|
| len(nums) | How many elements are present? | A countable list | 6 |
| max(nums) | Which element is largest? | Elements that can be compared | 74 |
| min(nums) | Which element is smallest? | Elements that can be compared | 3 |
| sum(nums) | What is the total of the elements? | Numeric elements | 154 |
The four functions answer different questions about the same list.
len() counts the elements in a list. max() examines the elements and returns the largest one. min() examines the elements and returns the smallest one. sum() adds the elements and returns their total. These operations normally require Python to examine the list's elements, but the traversal is handled automatically rather than written as a loop by you.
Following max() Through a List
To understand what max() is doing, imagine its automatic traversal of [3, 41, 12, 9, 74, 15]. It begins with 3 as the largest value seen so far. It then checks 41 and replaces 3 because 41 is larger. The values 12 and 9 do not replace 41. When the function reaches 74, it updates the largest value again. Finally, 15 does not replace 74, so max() returns 74.
What do you think happens?
What will these calls return for values = [8, 2, 11, 5]?
Reveal answer
len(values) is 4, max(values) is 11, min(values) is 2, and sum(values) is 26
There are four elements. Comparing them identifies 11 as the largest and 2 as the smallest. Adding them produces 26.
Averages from Two Results
- 1Use
sum(list)to calculate the total of the numeric elements. - 2Use
len(list)to count the elements. - 3Divide the total by the count.
Computing an Average
Find the average of scores = [10, 20, 30, 40].
- 1Find the total
sum(scores)adds the numeric elements and returns 100. - 2Count the elements
len(scores)counts the four elements and returns 4. - 3Divide
Divide 100 by 4 to obtain the average.
The average is 25.
25.0Boolean List Checks
Python also provides any() and all() for lists of boolean values or conditions. any() returns True when at least one element is True. all() returns True only when every element is True. If every element is True, both return True. If one element is False, any() can remain True while all() returns False. If every element is False, both return False.
| Boolean list | any() | all() |
|---|---|---|
| [True, True] | True | True |
| [True, False] | True | False |
| [False, False] | False | False |
any() asks whether at least one value is True; all() asks whether every value is True.
Mistakes in List Analysis
Common mistake Using sum() on nonnumeric elements
What it looks likenames = ["Ana", "Bo"]; sum(names)Why it failsThe source material states that
sum()requires numeric elements.Do this insteadUse sum() for a list of numbers, or choose a different operation for other data.
Common mistake Confusing the largest value with the number of elements
What it looks likevalues = [8, 2, 11, 5]Why it failsmax(values)finds the largest element, whilelen(values)counts the elements.Do this insteadUse max() for the largest value and len() for the item count.
Common mistake Expecting min() or max() to calculate a total
What it looks likevalues = [8, 2, 11, 5]Why it failsmin()andmax()compare elements; neither adds them.Do this insteadUse sum() when the required result is the total of numeric elements.
Common mistake Using any() when every condition must be true
What it looks likechecks = [True, False]; any(checks)Why it failsany()needs only one True value, so it returns True even though one element is False.Do this insteadUse all() when every element must be True.
Common mistake Writing a manual loop for a standard built-in operation
Why it failsThe documented built-in functions perform common list analysis without requiring you to write the traversal yourself.
Do this insteadPrefer len(), max(), min(), and sum() when they directly solve the problem.
Before calling a built-in function, identify the question you need to answer. Use len() for a count, max() for the largest comparable element, min() for the smallest comparable element, and sum() for the total of numeric elements. For an average, combine sum() and len().
Practice and Review
Given readings = [12, 18, 9, 21], determine the result of len(readings), max(readings), min(readings), sum(readings), and sum(readings) / len(readings). Then explain which function you would choose to answer each question.
Hints
- Count the elements before calculating the average.
- Compare the values to find the largest and smallest.
- Add the values for the total.
- 1
len()counts list elements.max()returns the largest comparable element, andmin()returns the smallest comparable element.sum()adds numeric elements. Combiningsum(list)/len(list)computes an average.any()checks whether at least one boolean value is True, whileall()checks whether every value is True. These built-in functions are preferred for standard list-analysis tasks because they are shorter to write and avoid a manually written traversal.
Key Takeaways
- Use
len(),max(),min(), andsum()to analyze lists without writing manual loops. len()counts elements,max()andmin()compare elements, andsum()adds numeric elements.- Compute an average by dividing
sum(list)bylen(list). - Use
any()for at least one True value andall()when every value must be True. - Choose a function based on the type of result and the kind of data in the list.