The min() and max() functions are built-in Python functions used to find the smallest and largest values from an iterable or among multiple arguments.
They are useful for:
- Finding highest and lowest marks
- Finding minimum and maximum prices
- Finding smallest and largest numbers
- Working with strings
- Finding values using a custom
key - Processing lists of dictionaries
1. min() Function
The min() function returns the smallest value.
Syntax
min(iterable)Example:
numbers = [10, 5, 20, 3, 15]
result = min(numbers)
print(result)Output:
32. max() Function
The max() function returns the largest value.
numbers = [10, 5, 20, 3, 15]
result = max(numbers)
print(result)Output:
203. Using min() and max() Together
numbers = [10, 5, 20, 3, 15]
print("Minimum:", min(numbers))
print("Maximum:", max(numbers))Output:
Minimum: 3
Maximum: 204. Find Minimum from a Tuple
numbers = (50, 20, 80, 10, 40)
print(min(numbers))Output:
105. Find Maximum from a Tuple
numbers = (50, 20, 80, 10, 40)
print(max(numbers))Output:
806. Find Minimum from a Set
numbers = {50, 20, 80, 10, 40}
print(min(numbers))Output:
107. Find Maximum from a Set
numbers = {50, 20, 80, 10, 40}
print(max(numbers))Output:
808. Using Multiple Arguments
You don't always need a list.
result = min(10, 5, 20, 3, 15)
print(result)Output:
3Similarly:
result = max(10, 5, 20, 3, 15)
print(result)Output:
209. min() with Strings
min() can find the string that comes first according to Python's string comparison.
names = ["Ram", "Sita", "Hari", "Gita"]
print(min(names))Output:
Gita10. max() with Strings
names = ["Ram", "Sita", "Hari", "Gita"]
print(max(names))Output:
Sita11. Find Shortest String
Use the key parameter.
words = ["Python", "AI", "Django", "Web"]
result = min(
words,
key=len
)
print(result)Output:
AIHere Python compares the length of each string.
12. Find Longest String
words = ["Python", "AI", "Django", "Web"]
result = max(
words,
key=len
)
print(result)Output:
Django13. min() with key
The key parameter tells Python what value to use for comparison.
names = ["Rajendra", "Ram", "Sita", "Hari"]
result = min(
names,
key=len
)
print(result)Output:
RamThe lengths are:
Rajendra → 8
Ram → 3
Sita → 4
Hari → 4So "Ram" is selected.
14. max() with key
names = ["Rajendra", "Ram", "Sita", "Hari"]
result = max(
names,
key=len
)
print(result)Output:
Rajendra15. Find Student with Highest Marks
students = [
("Ram", 75),
("Sita", 85),
("Hari", 65),
("Gita", 90)
]
result = max(
students,
key=lambda student: student[1]
)
print(result)Output:
('Gita', 90)16. Find Student with Lowest Marks
students = [
("Ram", 75),
("Sita", 85),
("Hari", 65),
("Gita", 90)
]
result = min(
students,
key=lambda student: student[1]
)
print(result)Output:
('Hari', 65)17. Get Only the Student Name
students = [
("Ram", 75),
("Sita", 85),
("Hari", 65),
("Gita", 90)
]
highest = max(
students,
key=lambda student: student[1]
)
print(highest[0])Output:
Gita18. Get Highest Mark
highest = max(
students,
key=lambda student: student[1]
)
print(highest[1])Output:
9019. Find Highest-Priced Product
products = [
("Laptop", 80000),
("Mouse", 1500),
("Keyboard", 3000),
("Monitor", 25000)
]
result = max(
products,
key=lambda product: product[1]
)
print(result)Output:
('Laptop', 80000)20. Find Cheapest Product
result = min(
products,
key=lambda product: product[1]
)
print(result)Output:
('Mouse', 1500)21. min() and max() with Dictionaries
Consider:
students = {
"Ram": 75,
"Sita": 85,
"Hari": 65,
"Gita": 90
}To find the student with the highest mark:
highest = max(
students,
key=students.get
)
print(highest)Output:
Gita22. Find Student with Lowest Mark
lowest = min(
students,
key=students.get
)
print(lowest)Output:
Hari23. Get Highest Mark from Dictionary
highest_mark = max(
students.values()
)
print(highest_mark)Output:
9024. Get Lowest Mark from Dictionary
lowest_mark = min(
students.values()
)
print(lowest_mark)Output:
6525. Find Highest Value and Key
students = {
"Ram": 75,
"Sita": 85,
"Hari": 65,
"Gita": 90
}
name = max(
students,
key=students.get
)
mark = students[name]
print(name)
print(mark)Output:
Gita
9026. List of Dictionaries
This is very common when working with API data.
students = [
{"name": "Ram", "marks": 75},
{"name": "Sita", "marks": 85},
{"name": "Hari", "marks": 65},
{"name": "Gita", "marks": 90}
]
highest = max(
students,
key=lambda student: student["marks"]
)
print(highest)Output:
{'name': 'Gita', 'marks': 90}27. Find Lowest Student
lowest = min(
students,
key=lambda student: student["marks"]
)
print(lowest)Output:
{'name': 'Hari', 'marks': 65}28. Find Maximum Absolute Value
numbers = [-10, 5, -30, 20, -2]
result = max(
numbers,
key=abs
)
print(result)Output:
-30The absolute values are:
10, 5, 30, 20, 2So -30 has the largest absolute value.
29. Find Minimum Absolute Value
result = min(
numbers,
key=abs
)
print(result)Output:
-230. Find Highest Number After Transformation
You can use a lambda with max().
numbers = [2, 3, 4, 5]
result = max(
numbers,
key=lambda x: x ** 2
)
print(result)Output:
5Python compares:
2² = 4
3² = 9
4² = 16
5² = 2531. Find Longest Course Name
courses = [
"Python",
"Django",
"MERN",
"Data Science"
]
result = max(
courses,
key=len
)
print(result)Output:
Data Science32. Find Shortest Course Name
result = min(
courses,
key=len
)
print(result)Output:
MERN33. Combine min() with filter()
You can combine the functions you've already learned.
numbers = [10, 25, 40, 15, 30, 5]
result = min(
filter(
lambda x: x >= 20,
numbers
)
)
print(result)Output:
25First filter() selects:
25, 40, 30Then min() finds:
2534. Combine max() with filter()
numbers = [10, 25, 40, 15, 30, 5]
result = max(
filter(
lambda x: x >= 20,
numbers
)
)
print(result)Output:
4035. Combine max() with zip()
This is useful for finding the highest value with its associated name.
names = ["Ram", "Sita", "Hari", "Gita"]
marks = [75, 85, 65, 90]
result = max(
zip(marks, names)
)
print(result)Output:
(90, 'Gita')36. Combine min() with zip()
result = min(
zip(marks, names)
)
print(result)Output:
(65, 'Hari')This is a very useful pattern:
max(zip(values, names))or:
min(zip(values, names))37. Find Highest and Lowest Marks
marks = [75, 85, 65, 90, 55]
highest = max(marks)
lowest = min(marks)
print("Highest:", highest)
print("Lowest:", lowest)Output:
Highest: 90
Lowest: 5538. Calculate Range of Values
You can use both functions to find the difference between the highest and lowest values.
marks = [75, 85, 65, 90, 55]
result = max(marks) - min(marks)
print(result)Output:
35This is called the range of the values.
39. Practical Example : Employee Salaries
employees = {
"Ram": 35000,
"Sita": 45000,
"Hari": 30000,
"Gita": 55000
}
highest = max(
employees,
key=employees.get
)
lowest = min(
employees,
key=employees.get
)
print("Highest:", highest)
print("Lowest:", lowest)Output:
Highest: Gita
Lowest: Hari40. Practical Example : Course Prices
courses = {
"Python": 15000,
"Django": 18000,
"MERN": 22000,
"Data Science": 25000
}
cheapest = min(
courses,
key=courses.get
)
expensive = max(
courses,
key=courses.get
)
print("Cheapest:", cheapest)
print("Most Expensive:", expensive)Output:
Cheapest: Python
Most Expensive: Data Science41. Important Difference Between min(), max() and sorted()
You have already learned sorted().
numbers = [5, 2, 8, 1, 3]min()
min(numbers)Returns:
1max()
max(numbers)Returns:
8sorted()
sorted(numbers)Returns:
[1, 2, 3, 5, 8]Remember:
min() → smallest value
max() → largest value
sorted() → all values in sorted order42. Important Points to Remember
Find smallest
min(numbers)Find largest
max(numbers)Multiple values
min(10, 20, 5)
max(10, 20, 5)Custom comparison
max(names, key=len)With lambda
max(
students,
key=lambda student: student[1]
)With dictionary
max(
students,
key=students.get
)With zip()
max(
zip(marks, names)
)Easy way to remember
min() → LOWEST
max() → HIGHEST
sorted() → ORDER EVERYTHING