Skip to main content

max()

The max() function in Python is a built-in function that returns the highest value among the arguments passed to it. It can take multiple arguments or an iterable (such as a list, tuple, or set) and return the maximum value present in them based on their comparison order. If the arguments are of different types, it will raise a TypeError.

Parameter Values

Parameter Description
iterable

An iterable (list, tuple, dictionary, etc.) containing values to compare and return the maximum value from.

default

An optional parameter specifying the default value to return if the iterable is empty.

*args

Additional iterables or values to compare and return the maximum value from.

key

A function that will be applied to each element before comparison to determine the maximum value.

Return Values

The max() function can return any data type present in the iterable argument.

How to Use max() in Python

Example 1:

The max() function returns the largest item in an iterable or the largest of two or more arguments.

max(1, 2, 3, 4, 5)
Example 2:

The max() function can be used with key parameter to specify a function that is used to extract a comparison key from each element.

max(['apple', 'banana', 'strawberry', 'cherry'], key=len)
Example 3:

The max() function can accept an iterable and a default value to return if the iterable is empty.

max([], default='No values')