Python max Function

The max() function is used to return the maximum value for two or more arguments passed to it.

If an iterable e.g. 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])

A few important points to note about max() function:

  • The largest item in the iterable like a 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.
  • 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 the error can be avoided.

See the examples below for its usage.

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:

The maximum number is: 8100

You can see, the maximum number 8100 is displayed.

An example of using a list with the max 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, the 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:

The max value in the list: 111

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

Without key argument, the maximum list: [10, ‘x’, ‘y’, 15, 20]
Using key argument, the maximum list: [5, 7, ‘a’, 25, ‘b’, ‘c’]

This is how it worked:

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 solve the mysteries of coding together!