Python max() function returns the largest item in the iterable or the largest of two or more arguments.
Python max()
Python max() function syntax is:
max(iterable, *[, key, default])
max(arg1, arg2, *args[, key])
- If there is only one argument, it should be an iterable such as string, list, tuple etc. The largest item in the iterable is returned.
- If two or more arguments are provided, largest of them will be returned.
- We can specify
key
argument function to be used for identifying the largest item. This is an optional argument and mostly used when arguments are custom objects. - The
default
argument specifies an object to return if the provided iterable is empty. If the iterable is empty and default is not provided,ValueError
exception is raised. - If multiple largest element is found, then the first one is returned.
Python max() function examples
Let’s look at some examples of max() function.
max() with string
When max() function is used with string argument, the character with maximum unicode value is returned.
s = 'abcCba'
print(max(s))
print('c' > 'C')
Output:
c
True
max() with tuple
tuple_numbers = (1, 2, 3, 4)
print(max(tuple_numbers))
Output: 4
max of list
list_numbers = [1, 2, 3, -4]
print(max(list_numbers))
Output: 3
max() of objects
When we want to use max() function with custom objects, we have to provide key
function argument to be used for comparing the objects.
class Data:
id = 0
def __init__(self, i):
self.id = i
def __str__(self):
return 'Data[%s]' % self.id
def get_data_id(data):
return data.id
# max() with objects and key argument
list_objects = [Data(1), Data(2), Data(-10)]
print(max(list_objects, key=get_data_id))
Output: Data[2]
If we don’t provide a key function as an argument, we will get the following error.
TypeError: '>' not supported between instances of 'Data' and 'Data'
max() with empty iterable and default value
print(max([], default=20))
Output: 20
max() function with multiple arguments
print(max(1, 2, 3, 4))
Output: 4
max() with arguments and key function
def str_length(s):
return len(s)
print(max('a', 'abc', 'ab', key=str_length))
Output: abc
max() with multiple iterables
x1 = [10, 20, 30]
x2 = [5, 15, 40, 25]
print(max(x1, x2, key=len))
Output: [5, 15, 40, 25]
If we don’t provide key function as an argument, the output will be [10, 20, 30]
. It’s because the comparison will be done between the elements of the iterable elements one by one. When an element with the larger value is found, the iterable with that element will be returned.
max() with multiple iterables of objects
x1 = [Data(10), Data(20), Data(30)]
x2 = [Data(5), Data(15), Data(40), Data(25)]
max_list = max(x1, x2, key=len)
for x in max_list:
print(x)
Output:
Data[5]
Data[15]
Data[40]
Data[25]
Notice that with multiple arguments, iterables are treated as objects. If we don’t specify key function, we will get error message as TypeError: '>' not supported between instances of 'Data' and 'Data'
. Earlier it worked with integer elements because they support > and < operators.
Summary
Python max() function helps us in identifying the largest element in the iterable or largest item from multiple arguments. It’s useful because we can specify our own function to be used for comparison through key argument.
Reference: Official Documentation