Concepts / Sorting and Ordering Data in Python

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.

  • Programming

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.

Key point

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.

7largest 7; smallest 73largest 7; smallest 312largest 12; smallest 35largest 12; smallest 3compare 3compare 12compare 5
How do the current largest and smallest values change as Python examines the values 7, 3, 12, and 5?
Worked example

Numeric extremes

Find the largest and smallest values in the list [7, 3, 12, 5].

  1. 1
    Compare the values

    The values are ordered numerically. The largest value is 12, and the smallest value is 3.

  2. 2
    Apply the functions

    max([7, 3, 12, 5]) selects 12, while min([7, 3, 12, 5]) selects 3.

Result

The maximum is 12 and the minimum is 3.

python
Output
12
3

Ordering 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.

spacesmallest hereHuppercaseelowercasewlargest herecomes beforecomes beforecomes before
How are a space, an uppercase letter, and lowercase letters ordered when max() and min() compare characters?

What do you think happens?

What do you predict max("Hello world") and min("Hello world") will return?

  • max returns w and min returns a capital letter

  • max returns w and min returns the space

  • max returns the whole string and min returns an empty string

Reveal answer
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.

python
Output
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.

Worked example

Numbers and characters use different orderings

Explain why max([4, 18, 9]) and max("bAc") use different comparison rules.

  1. 1
    Numeric list

    The list contains numbers, so Python uses numeric value. 18 is greater than 4 and 9.

  2. 2
    String

    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.

  3. 3
    Interpret the result

    The functions do not apply a universal everyday alphabetical rule. They select according to the ordering appropriate to the values being compared.

Result

max([4, 18, 9]) is 18, while max("bAc") is c.

Best practice

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 like
    values = [7, 3, 12, 5]; result = max(values)
    Why it fails

    The function returns one value from the collection, not a rearranged list.

    Do this instead

    Treat 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 like
    values = [7, 3, 12, 5]
    Why it fails

    The largest value is determined by comparison, not by position. The last item is 5, but the maximum is 12.

    Do this instead

    Compare the values according to their ordering.

  • Common mistake

    Treating character ordering as ordinary alphabetical order.

    What it looks like
    text = "Hello world"
    Why it fails

    Spaces, uppercase letters, and lowercase letters occupy different positions in Python's standard character ordering.

    Do this instead

    Remember 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 fails

    There is no value available to return when the collection is empty.

    Do this instead

    Ensure that the collection contains a value before asking for its maximum or minimum.

Practice Before You Run

MEDIUM

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.
Worked example

Practice answers

Find the maximum and minimum for [14, 2, 9], "Cab", and "room".

  1. 1
    Numeric list

    Numeric ordering makes 14 the maximum and 2 the minimum.

  2. 2
    String "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.

  3. 3
    String "room"

    All characters are lowercase, so r is greatest and m is smallest among the characters present.

Result

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, while min() 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.

Related concepts

More concepts starting with S