Python max()

The max() method returns the largest element in an iterable or largest of two or more parameters.

Differnt syntaxes of max() are:

max(iterable, *iterables[,key, default])
max(arg1, arg2, *args[, key])

If you want to find the smallest element, use min() method.


max() Parameters

max() has two forms of arguments it can work with.

  1. max(iterable, *iterables[, key, default])
    • iterable - sequence (tuple, string), collection (set, dictionary) or an iterator object whose largest element is to be found
    • *iterables (Optional) - any number of iterables whose largest is to be found
    • key (Optional) - key function where the iterables are passed and comparison is performed based on its return value
    • default (Optional) - default value if the given iterable is empty
  2. max(arg1, arg2, *args[, key])
    • arg1 - mandatory first object for comparison (could be number, string or other object)
    • arg2 - mandatory second object for comparison (could be number, string or other object)
    • *args​ (Optional) - other objects for comparison
    • key - key function where each argument is passed, and comparison is performed based on its return value

Return value from max()

The max() method returns:

1. max(iterable, *iterables[, key, default])

max() method return value for max(iterable, *iterables[, key, default])
Case Key Default Return value
Empty iterable   No Yes No Raises ValueError exception
Empty iterable Yes Yes Returns the default value
Single iterable (Not empty) No No Yes Returns the largest among the iterable
Single iterable (Not empty) Yes No Yes Passes each element in the iterable to the key function Returns largest element based on the return value of the key function
Multiple iterable (Not empty) No No Yes Returns largest among the given iterables
Multiple iterable (Not empty) Yes No Yes Passes each iterable to the key function Returns largest iterable based on the return value of the key function

2. max(arg1, arg2, *args[, key])

max() method return value for max(arg1, arg2, *args[, key])
Case Key Return value
First and second argument passed No   Returns largest among the given arguments
First and second argument passed Yes Passes the arguments to the key function Returns largest among the arguments based on the return value of the key function
More than 2 arguments passed No Returns largest among the given arguments
More than 2 arguments passed Yes Passes each arugment to the key function Returns largest among the arguments based on the return value of the key function

Example 1: Find maximum among the given numbers

# using max(arg1, arg2, *args)
print('Maximum is:', max(1, 3, 2, 5, 4))

# using max(iterable)
num = [1, 3, 2, 8, 5, 10, 6]
print('Maximum is:', max(num))

When you run the program, the output will be:

Maximum is: 5
Maximum is: 10

Example 2: Find number whose sum of digits is largest using key function

def sumDigit(num):
    sum = 0
    while(num):
        sum += num % 10
        num = int(num / 10)
    return sum

# using max(arg1, arg2, *args, key)
print('Maximum is:', max(100, 321, 267, 59, 40, key=sumDigit))

# using max(iterable, key)
num = [15, 300, 2700, 821, 52, 10, 6]
print('Maximum is:', max(num, key=sumDigit))

When you run the program, the output will be:

Maximum is: 267
Maximum is: 821

Here, each element in the passed argument (list or argument) is passed to the same function sumDigit().

Based on the return value of the sumDigit(), i.e. sum of the digits, the largest is returned.


Example 3: Find list with maximum length using key function

num = [15, 300, 2700, 821]
num1 = [12, 2]
num2 = [34, 567, 78]

# using max(iterable, *iterables, key)
print('Maximum is:', max(num, num1, num2, key=len))

When you run the program, the output will be:

Maximum is: [15, 300, 2700, 821]

In this program, each iterable num, num1 and num2 is passed to the built-in method len().

Based on the result, i.e. length of each list, the list with maximum length is returned.