Sorting and Ordering Data in Python
max() selects the largest value according to Python's ordering. min() selects the smallest value according to that same ordering.
Finding the Two Extremes
A collection may contain many values, but you may only need its two endpoints: the largest value and the smallest value. Python provides the built-in functions max() and min() for this purpose. Each function examines the values in a collection and returns one value from that collection.
max() selects the largest value according to Python's ordering. min() selects the smallest value according to that same ordering.
Tracing Numeric Candidates
For numbers, Python's ordering follows the number line. The largest number becomes the maximum, and the smallest number becomes the minimum. A useful mental trace is to keep a current candidate while examining the collection: a larger number replaces the current maximum candidate, while a smaller number replaces the current minimum candidate.
Numeric extremes
Find the largest and smallest values in the list [7, 3, 12, 5].
- 1Compare the values
The values are ordered numerically. The largest value is 12, and the smallest value is 3.
- 2Apply the functions
max([7, 3, 12, 5])selects 12, whilemin([7, 3, 12, 5])selects 3.
The maximum is 12 and the minimum is 3.
12
3Ordering Characters
max() and min() can also receive a string. In that situation, Python compares the individual characters in the string and returns one character, rather than returning a new string or rearranging the characters.
Character comparison follows Python's standard ordering: spaces come before uppercase letters, and uppercase letters come before lowercase letters. Therefore, character ordering is not the same as a person's usual idea of alphabetical order.
What do you think happens?
What do you predict max("Hello world") and min("Hello world") will return?
Reveal answer
max("Hello world") returns w, and min("Hello world") returns the space character.
The space is before uppercase letters, uppercase letters are before lowercase letters, and w is the greatest lowercase character present in the string.
w
Reading the Returned Value
The type of result depends on the collection's contents. With a numeric list, max() or min() returns a number from that list. With a string, the function returns one character from the string. In both cases, the function returns a single value rather than a collection.
Numbers and characters use different orderings
Explain why max([4, 18, 9]) and max("bAc") use different comparison rules.
- 1Numeric list
The list contains numbers, so Python uses numeric value. 18 is greater than 4 and 9.
- 2String
The string contains characters, so Python uses character ordering. The uppercase A comes before lowercase b and c, making c the greatest character among these three.
- 3Interpret the result
The functions do not apply a universal everyday alphabetical rule. They select according to the ordering appropriate to the values being compared.
max([4, 18, 9]) is 18, while max("bAc") is c.
Before predicting a result, identify what is being compared. For numbers, think about position on the number line. For strings, think about individual characters and the ordering of spaces, uppercase letters, and lowercase letters.
Mistakes with Extremes
Common mistake Expecting max() or min() to return a sorted collection.
What it looks likevalues = [7, 3, 12, 5]; result = max(values)Why it failsThe function returns one value from the collection, not a rearranged list.
Do this insteadTreat result as the single largest value, which is 12 in this example.
Common mistake Assuming that maximum means the last item in the collection.
What it looks likevalues = [7, 3, 12, 5]Why it failsThe largest value is determined by comparison, not by position. The last item is 5, but the maximum is 12.
Do this insteadCompare the values according to their ordering.
Common mistake Treating character ordering as ordinary alphabetical order.
What it looks liketext = "Hello world"Why it failsSpaces, uppercase letters, and lowercase letters occupy different positions in Python's standard character ordering.
Do this insteadRemember that spaces come before uppercase letters, which come before lowercase letters.
Common mistake Calling max() or min() on an empty collection without handling that case.
Why it failsThere is no value available to return when the collection is empty.
Do this insteadEnsure that the collection contains a value before asking for its maximum or minimum.
Practice Before You Run
For each collection, predict the result of max() and min() before checking your answer: [14, 2, 9], "Cab", and "room".
Hints
- Use numeric ordering for the list.
- For strings, compare individual characters rather than whole words.
- Remember that uppercase letters come before lowercase letters.
Practice answers
Find the maximum and minimum for [14, 2, 9], "Cab", and "room".
- 1Numeric list
Numeric ordering makes 14 the maximum and 2 the minimum.
- 2String "Cab"
C is an uppercase letter, while a and b are lowercase. The space rule is not involved here. Among these characters, b is greatest and C is smallest.
- 3String "room"
All characters are lowercase, so r is greatest and m is smallest among the characters present.
The pairs are max([14, 2, 9]) = 14 and min([14, 2, 9]) = 2; max("Cab") = b and min("Cab") = C; max("room") = r and min("room") = m.
Key Takeaways
max()returns the largest value in a collection, whilemin()returns the smallest.- Both functions return one value rather than a new sorted collection.
- Numbers are compared by numeric value.
- Strings are examined character by character using Python's standard ordering.
- Spaces come before uppercase letters, and uppercase letters come before lowercase letters.