Python max function

The max() function is used to return the maximum value for two or more arguments passed to it. If an iterable like a list is passed as an argument, it will return the largest item in the iterable.

Sum with numbers example  sum with list

How to use the max function?

The general syntax of using the max function is as follows.

For numbers:

max (num1, num2, num3, *args[, key])

In that case, the largest number will be returned.

The key is optional where each argument is passed to the key function. The comparison is performed based on its return value.

For iterable:

max(iterable, *[, key, default])

The largest item in the iterable like list, tuple etc. will be returned. If two or more arguments are passed (e.g. two or more lists, tuple or other iterables) the max function will return the largest argument. That means, it returns the largest object. See an example of two lists.

The key argument is optional. It specifies a one-argument ordering function. See the example below for its usage.

If the iterarable is empty then ValueError will be raised. In that case, you may use the default optional argument that specifies an object to return. If the provided iterator is empty then default object will be returned and error can be avoided.

An example of Python max function with number arguments

In this example, six numbers are passed as the arguments in the max() function. The returned value is assigned to a variable and displayed:

See online demo and code

Python max numbers

You can see, the maximum number 8100 is displayed.

A demo of using list with max Python function

For this example, a list of numeric values is created and passed to the max() function as an argument. The list contains five numeric objects. See the code and output:

See online demo and code


Python max

An example of key argument with a list: max(iterable, key)

For this example of demonstrating the key argument, a list is created that contains mixed items. The numbers are in the form of strings, float and integer items.

The max() function is used where that list is passed and the key argument is used. The lambda function is used where each item of the list is converted to int thus max function will return the maximum value.

See online demo and code

Python max key

If the list was used as in the previous example, an exception would have raised because the list contains string elements.

An example of multiple iterables with key: max(iterable, *iterables, key)

As mentioned earlier, if two or more iterables are passed the largest argument/iterable as object is returned.

In this example, two lists are created with six and five elements, respectively. The max function is used two ways. First, simply sending the two lists as arguments and the second uses the key argument along with the len() function. See the code and output:

See online demo and code


Python max key list

In the first case, the first items of both lists are compared and the max() function returned the list with the larger item. In that case, 10 is greater than 5 so the second list is returned though it has five items.

In the second case, the len() function is used and max returned the list with the larger number of items. As the first list contains six items, so this is returned and displayed.