Examples
Example 1: Finding the Maximum Value in a List
Python
numbers = [1, 3, 5, 2, 8]
max_value = max(numbers)
print(max_value)

You can also try this code with Online Python Compiler
Run Code
Output
8
In this example, max() finds the largest number in the list.
Example 2: Finding the Maximum of Multiple Arguments
Python
max_value = max(4, 7, 1, 9)
print(max_value)

You can also try this code with Online Python Compiler
Run Code
Output
9
Here, max() compares the numbers directly and returns the largest one.
Example 3: Using the key Parameter with Custom Objects
Consider a list of dictionaries where each dictionary represents a person with name and age:
Python
people = [
{'name': 'Alice', 'age': 25},
{'name': 'Bob', 'age': 30},
{'name': 'Charlie', 'age': 22}
]
oldest = max(people, key=lambda person: person['age'])
print(oldest)

You can also try this code with Online Python Compiler
Run Code
Output
{'name': 'Bob', 'age': 30}
Here, max() finds the dictionary with the highest age value.
max() Function with Iterable in Python
Syntax
max(iterable, key=None, default=None)
Parameters
- iterable: This is the iterable (like a list, tuple, or set) from which to find the maximum value.
- key: This is an optional function to compute a comparison key from each item.
- default: This is an optional value to return if the iterable is empty. If not provided and the iterable is empty, max() raises a ValueError.
Return
The max() function returns the largest item in the iterable. If the iterable is empty and no default is provided, it raises a ValueError.
Examples
Example 1: Finding the Maximum Value in a List
Python
numbers = [10, 20, 4, 45, 99]
max_value = max(numbers)
print(max_value)

You can also try this code with Online Python Compiler
Run Code
Output
99
Here, max() returns the highest number from the list.
Example 2: Finding the Maximum Value with a Key Function
Suppose we have a list of tuples where each tuple represents a person’s name and score:
Python
scores = [('Alice', 85), ('Bob', 90), ('Charlie', 78)]
highest_score = max(scores, key=lambda x: x[1])
print(highest_score)

You can also try this code with Online Python Compiler
Run Code
Output
('Bob', 90)
In this example, max() uses the key parameter to find the tuple with the highest score.
Example 3: Using default with an Empty Iterable
Python
empty_list = []
max_value = max(empty_list, default='No values')
print(max_value)

You can also try this code with Online Python Compiler
Run Code
Output
No values
When the list is empty, max() returns the default value specified.
Frequently Asked Questions
Can max() handle both numbers and strings?
Yes, max() can handle numbers, strings, and other data types, as long as they are comparable.
How do I find the maximum value based on a specific attribute?
Use the key parameter with a lambda function or another function that extracts the attribute for comparison.
What happens if max() is used on an empty iterable without a default value?
max() raises a ValueError. Provide a default value to avoid this issue.
Can max() work with a list of dictionaries?
Yes, use the key parameter to specify which dictionary field to use for comparison.
Conclusion
The max() function in Python is a powerful tool for finding the largest item in a collection of values or objects. Whether you're working with simple numbers or more complex data structures, max() provides flexibility with its key and default parameters. Understanding how to use max() effectively can help streamline your data analysis and decision-making processes.
Recommended Readings: