The max() and min() functions help you find the biggest or smallest value in a group of items quickly.
max() and min() in Python
Start learning this pattern below
Jump into concepts and practice - no test required
or
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Introduction
Syntax
Python
max(iterable, *[, key, default]) min(iterable, *[, key, default]) # Or with multiple arguments: max(arg1, arg2, *args, *[, key]) min(arg1, arg2, *args, *[, key])
You can give max() or min() either a list (or any iterable) or multiple separate values.
The optional key lets you decide how to compare items, like by length or a property.
Examples
Python
max([3, 7, 2, 5])
Python
min(10, 4, 8, 1)
Python
max(['apple', 'banana', 'pear'], key=len)
Python
min([], default=0)
Sample Program
This program finds the biggest and smallest numbers from a list, then finds the longest and shortest words from another list.
Python
numbers = [10, 20, 5, 40, 25] print("Max number:", max(numbers)) print("Min number:", min(numbers)) words = ['cat', 'elephant', 'dog'] print("Longest word:", max(words, key=len)) print("Shortest word:", min(words, key=len))
Important Notes
If you use max() or min() on an empty list without a default, Python will give an error.
The key parameter is very useful when working with complex data like lists of strings or objects.
Summary
max() finds the biggest item; min() finds the smallest.
You can use them with lists or separate values.
Use key to customize how items are compared.
Practice
1. What does the
max() function do in Python?easy
Solution
Step 1: Understand the purpose of max()
Themax()function is used to find the biggest or largest item from a list or multiple values.Step 2: Compare with other options
Options A, C, and D describe different functions or actions, not whatmax()does.Final Answer:
Finds the largest item in a list or among values -> Option AQuick Check:
max() = largest item [OK]
Hint: max() returns the biggest value from inputs [OK]
Common Mistakes:
- Confusing max() with min()
- Thinking max() sorts the list
- Assuming max() removes duplicates
2. Which of the following is the correct way to find the minimum value in a list named
nums?easy
Solution
Step 1: Recall the syntax of min()
The correct syntax to find the smallest value in a list ismin(list_name). So,min(nums)is correct.Step 2: Check other options for errors
minimum(nums) uses a non-existent functionminimum(). nums.min() tries to callmin()as a method on the list, which is invalid. min = nums assignsminto the list, which is incorrect usage.Final Answer:
min(nums) -> Option BQuick Check:
min(list) = smallest value [OK]
Hint: Use min(list_name) to get smallest value [OK]
Common Mistakes:
- Using min as a method like nums.min()
- Trying to assign min to a variable incorrectly
- Using a wrong function name like minimum()
3. What is the output of this code?
numbers = [3, 7, 2, 9, 5] print(max(numbers)) print(min(numbers))
medium
Solution
Step 1: Identify the list values
The listnumberscontains [3, 7, 2, 9, 5].Step 2: Find max and min values
The largest value is 9 and the smallest value is 2.Final Answer:
9 and 2 -> Option AQuick Check:
max = 9, min = 2 [OK]
Hint: max() and min() find biggest and smallest values [OK]
Common Mistakes:
- Mixing up max and min results
- Assuming print shows the whole list
- Syntax errors from missing parentheses
4. The following code gives an error. What is the problem?
values = [10, 20, 30] print(max(values, key=abs))
medium
Solution
Step 1: Understand max() with key parameter
Thekeyparameter lets max() compare items by a function result. Here,absreturns absolute values, which are the same as the original values since all are positive.Step 2: Check if code runs correctly
Sinceabsis a built-in function and the list is valid, the code runs without error and returns the largest value, 30.Final Answer:
The code runs without error and prints 30 -> Option CQuick Check:
max(values, key=abs) = 30 [OK]
Hint: max() with key=abs works fine; abs() is built-in [OK]
Common Mistakes:
- Thinking abs() is undefined
- Believing max() can't use key parameter
- Assuming code causes error
5. Given the list
words = ['apple', 'banana', 'grape', 'kiwi'], which code finds the word with the shortest length?hard
Solution
Step 1: Understand the key parameter with len()
Usingkey=lentellsmin()to compare words by their length, not alphabetically.Step 2: Identify shortest word
Among the words, 'kiwi' has the shortest length (4 letters). Somin(words, key=len)returns 'kiwi'.Final Answer:
min(words, key=len) -> Option DQuick Check:
min by length = 'kiwi' [OK]
Hint: Use min(list, key=len) to find shortest string [OK]
Common Mistakes:
- Using max() instead of min() for shortest
- Not using key=len and getting alphabetical result
- Assuming min(words) finds shortest word
