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.

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, tuples or other iterables) the max function will return the largest argument. That means it returns the largest object.

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 the 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:

Code:

# A max() function example with numbers



max_num = max(5000, 3500, 5500, 4000, 8100, 2500)

print('The maximum number is:', max_num)

Output:

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:

# A max() function example with a List

num_list = [5,10,40,2,15]


max_num_lst = max(num_list)

print('The maximum number in the list:', max_num_lst)

Output:

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.

Code:

mix_list = [10, '1','100',90, '111','2', 102.5 ]



mx_val=max(mix_list, key=lambda x:int(x))

print("The max value in the list: ",mx_val)

Result:

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:

lst1 = [5, 7, 'a', 25, 'b', 'c']

lst2 = [10, 'x', 'y', 15, 20]



#Simply passing three lists

print('Without key argument, the maximum list:', max(lst1, lst2))


#Using key argument

print('Using key argument, the maximum list:', max(lst1, lst2, key=len))

Output:

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.

Author - Atiq Zia

Atiq is the writer at jquery-az.com, an online tutorial website started in 2014. With a passion for coding and solutions, I navigate through various languages and frameworks. Follow along as we unravel the mysteries of coding together!