Python max()
Python max() builtin function is used to find the largest element in given iterable or among given arguments.
In this tutorial, we will learn about the syntax of Python max() function, and learn how to use this function with the help of examples.
Syntax
The syntax of max() function to find the largest element in an iterable is
max(iterable, *[, key, default])
where
Parameter | Required/Optional | Description |
---|---|---|
iterable | Required | A python iterable object. |
key | Optional | A Python function which takes an argument and returns a value. The returned value is used for comparison. |
default | Optional | A default value to return, if iterable is empty. |
The syntax of max() function to find the largest element among two ore more arguments is
max(arg1, arg2, *args[, key])
where
Parameter | Required/Optional | Description |
---|---|---|
arg1 | Required | A python object. First argument for comparison to find largest. |
arg2 | Required | A python object. Second argument for comparison to find largest. |
*args | Optional | More arguments for comparison to find the largest element. |
key | Optional | A Python function which takes an argument and returns a value. The returned value is used for comparison. |
Examples
1. max(iterable)
In this example, we will take an iterable, say list of numbers, and find the largest among the numbers in this list using max() builtin function.
Pass the list of numbers as argument to max() function. The function returns the largest number.
Python Program
myList = [4, 1, 8, 6, 0]
largest = max(myList)
print(f'Largest element in {myList} is {largest}.')
Output
Largest element in [4, 1, 8, 6, 0] is 8.
2. max(iterable, key)
In this example, we will pass a function for key parameter along with the list of numbers.
The key function we shall pass for named argument key
is abs() builtin function. We can also pass any user defined function that takes a single argument and returns a value.
Since, we have mentioned key function, the comparison happens based on the values returned by this key function for each element in the iterable.
Python Program
myList = [4, 1, -8, -6, 0]
largest = max(myList, key=abs)
print(f'Largest element in {myList} is {largest}.')
Output
Largest element in [4, 1, -8, -6, 0] is -8.
Absolute value of -8 is greater than that of any other element. So, max() function returned the element for which the returned value of key function is the largest.
3. max(iterable, default)
In this example, we will take an empty iterable, say an empty list, and pass this empty list along with default value to max() builtin function.
As the given iterable is empty, max() function returns the default value.
Python Program
myList = []
largest = max(myList, default=0)
print(f'Largest element in {myList} is {largest}.')
Output
Largest element in [] is 0.
4. max(arg1, arg2)
In this example, we will pass two arguments to max() builtin function to find the largest of these two arguments.
max() function returns the largest of these two arguments.
Python Program
arg1 = 4
arg2 = 8
largest = max(arg1, arg2)
print(f'Largest element of {arg1} and {arg2} is {largest}.')
Output
Largest element of 4 and 8 is 8.
5. max(arg1, arg2, *args)
In this example, we will pass more than two arguments to max() builtin function to find the largest of these multiple arguments.
max() function returns the largest of the given arguments.
Python Program
arg1 = 4
arg2 = 8
arg3 = 24
arg4 = 0
largest = max(arg1, arg2, arg3, arg4)
print(f'Largest element of given arguments is {largest}.')
Output
Largest element of given arguments is 24.
6. max(arg1, arg2, *args, key)
Similar to the case of max() function with iterable and key, we can also pass a key function along with arguments
max() function returns the largest of the given arguments based on the key function.
Python Program
arg1 = 4
arg2 = 8
arg3 = -24
arg4 = -16
largest = max(arg1, arg2, arg3, arg4, key=abs)
print(f'Largest element of given arguments is {largest}.')
Output
Largest element of given arguments is -24.
Conclusion
In this Python Tutorial, we have learnt the syntax of Python max() builtin function, and also learned how to use this function, with the help of Python example programs.