Concepts / Working with Lists and Collections

Working with Lists and Collections

max() and min() are built-in functions that find the largest and smallest values in any collection.

  • Programming

One Result from Many Values

A collection can contain many values, but sometimes you need only its largest or smallest member. Python provides the built-in functions max() and min() for this purpose. max() finds the largest value in a collection, while min() finds the smallest value. Each function returns one value from the collection rather than creating a new collection.

python
Output
largest is 19
smallest is 4

Tracing Numeric Extremes

For numbers, Python uses numeric value to decide which item is larger or smaller. You can understand the result by following the values in order and keeping track of the largest or smallest value found so far. In the list [12, 4, 19, 7], 19 becomes the maximum because it is larger than the other numbers, and 4 becomes the minimum because it is smaller than the other numbers.

comparecomparecomparereturn12current largest19maximum412 remains largest19new largest719 remains largest
As the numeric collection is examined, what happens to the current largest value?

Maximum and Minimum in a Numeric List

Find the largest and smallest values in the list [12, 4, 19, 7].

Apply max(): Compare the numbers by their numeric values. The largest value is 19.

Apply min(): Compare the numbers by their numeric values. The smallest value is 4.

Keep the return values: Each function produces one value, not a new list.

max([12, 4, 19, 7]) returns 19, and min([12, 4, 19, 7]) returns 4.

Numbers and Characters

The same max() and min() functions can work with numbers and strings, but the comparison rule depends on the values being compared. For numbers, Python follows numeric ordering: a larger number is greater and a smaller number is less. For characters in a string, Python follows its standard character ordering: spaces come before uppercase letters, and uppercase letters come before lowercase letters.

InputWhat Python comparesReturned result
A numeric listNumeric valuesThe largest or smallest number
A stringCharacters using standard character orderingThe largest or smallest character

Character Ordering in Strings

What do you think happens?

For the string 'Hello world', which character do you predict max() returns, and which character do you predict min() returns?

  • Maximum: w; minimum: space
  • Maximum: H; minimum: w
  • Maximum: o; minimum: H
Reveal answer

Answer: max('Hello world') returns 'w', and min('Hello world') returns a space character.

The string contains a space, uppercase letters, and lowercase letters. Standard character ordering places the space before the uppercase letters and the uppercase letters before the lowercase letters. Among the lowercase letters in this string, w is the largest character.

comes beforecomes beforew is laterspacesmallest hereHuppercaseelowercasewlargest here
When Python compares the characters in 'Hello world', which character is considered smaller or larger?
python
Output
largest_character is 'w'
smallest_character is ' '

Mistakes with Extreme Values

  • Expecting max() or min() to return a collection

    These functions return a single value from the collection.

    Fix: Store or use the returned value as one number or one character.

  • Calling the function on an empty collection

    There is no largest or smallest member when the collection is empty.

    Fix: Make sure the collection contains a value before applying the function.

  • Confusing character ordering with alphabetical order

    Python's stated character ordering places uppercase letters before lowercase letters.

    Fix: Remember the ordering sequence: spaces, uppercase letters, then lowercase letters.

Choosing the Right Comparison

Before using max() or min(), identify what the collection contains. If it contains numbers, reason along the number line. If it is a string, reason about individual characters and Python's standard character ordering. This small check helps you predict the result and prevents you from applying alphabetical expectations to character comparisons.

MEDIUM

Predict the result of max() and min() for the numeric list [8, 3, 15, 6]. Then predict what max() and min() return for the string "Cat". Explain which comparison rule you used in each case.

Hints
  • For the list, compare the values numerically.
  • For the string, consider the space-uppercase-lowercase ordering and the characters C, a, and t.

Key Takeaways

  1. max() finds the largest value in a collection, and min() finds the smallest.
  2. Both functions return one value from the collection rather than a new collection.
  3. Numbers are compared by numeric value.
  4. Characters in strings follow standard ordering: spaces, uppercase letters, then lowercase letters.
  5. An empty collection and assumptions about alphabetical order are common sources of mistakes.

Key Takeaways

  • Use max() and min() to select one extreme value from a collection.
  • Numeric collections use numeric ordering.
  • String collections use character ordering rather than ordinary alphabetical assumptions.
  • For the string 'Hello world', the maximum character is 'w' and the minimum character is a space.
  • Check for empty collections and remember that the result is a single value.