Python max() function

Updated on Jan 07, 2020


The max() function returns the largest of the input values.

Its syntax is as follows:

max(iterable[, default=obj, key=func]) -> value
PARAMETER        DESCRIPTION
iterable (required) An iterable object like string, list, tuple etc.
default (optional) The default value to return if the iterable is empty.
key (optional) It refers to the single argument function to customize the sort order. The function is applied to each item on the iterable.

or

max(a,b,c, ...[, key=func]) -> value
PARAMETER     DESCRIPTION
a, b, c ... Items to compare
key (optional) It refers to the single argument function to customize the sort order. The function is applied to each item on the iterable.

If max() is called with an iterable, it returns the largest item in it. If the iterable is empty then the default value is returned, otherwise, a ValueError exception is raised.

If max() is called with multiple arguments, it returns the largest one.

Let's see some examples:

Example 1: Calling max() with an iterable

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
>>> 
>>> max("abcDEF") # find largest item in the string
'c'
>>>
>>> 
>>> max([2, 1, 4, 3]) # find largest item in the list
4
>>> 
>>>
>>> max(("one", "two", "three")) # find largest item in the tuple
'two'
>>> 
>>> 
>>> max({1: "one", 2: "two", 3: "three"}) # find largest item in the dict
3
>>>
>>>
>>> max([]) # empty iterable causes ValueError
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: max() arg is an empty sequence
>>> 
>>> 
>>> max([], default=0) # supressing the error with default value
0
>>>

Try it out:

# find largest item in the string
print(max("abcDEF")) 

# find largest item in the list
print(max([2, 1, 4, 3])) 

# find largest item in the tuple
print(max(("one", "two", "three"))) 
'two'

# find largest item in the dict
print(max({1: "one", 2: "two", 3: "three"})) 
3

# empty iterable causes ValueError
# print(max([])) 

# supressing the error with default value
print(max([], default=0)) 


Example 2: Calling max() with multiple arguments

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
>>> 
>>> max(20, 10, 30, -5) 
30
>>>
>>>
>>> max("c", "b", "a", "Y", "Z")
'c'
>>>
>>> 
>>> max(3.14, -9.91, 2.41)
3.14
>>>

Trying to find largest value among the objects of different types causes an error.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
>>> 
>>> max(10, "pypi")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: unorderable types: str() > int()
>>> 
>>>
>>> max(5, [-10, 55])
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: unorderable types: list() > int()
>>>

Customing Sort Order #


To customize the sort order we use the key named argument. It works exactly like the key named argument of the sorted() function.

Here is an example where we use key argument to make the string comparision case-insentive.

1
2
3
4
5
6
7
>>>
>>> max("c", "b", "a", "Y", "Z")
'c'
>>> 
>>> max("c", "b", "a", "Y", "Z", key=str.lower)
'Z'
>>>

Try it out:

print(max("c", "b", "a", "Y", "Z"))

print(max("c", "b", "a", "Y", "Z", key=str.lower))


The following is another example where we compare strings based on its length instead of their ASCII values.

1
2
3
4
5
6
7
8
>>> 
>>> max(("python", "lua", "ruby"))
'ruby'
>>> 
>>> 
>>> max(("python", "lua", "ruby"), key=len)
'python'
>>>

Try it out:

print(max(("python", "lua", "ruby")))

print(max(("python", "lua", "ruby"), key=len))


There also exists a complementary function called min() which finds the lowest of the input values.


Other Tutorials (Sponsors)