Note
Python - max() - Find the biggest value
Overview of the Python max() function. Find the largest item in the iterable.
max(iterable_1, ..., key=func, default=obj) returns the largest item from a single iterable or the largest of two or more arguments.
Accepts optional key-word key argument that needs to be a function that will be used to evaluate each item's number/value.
If two elements or arguments have the same value max() will return the first one found.
max() also accepts optional key-word default argument that will be returned if provided iterable is empty.
my_list = [17, 0.5, 99]
my_max = max(my_list)
print(my_max) # 99
Output:
99
Using max() function with string values
my_list = ["www", "devcuriosity", "com"]
my_max = max(my_list)
print(my_max) # www
print(f'ord("w") = {ord("w")}, ord("d") = {ord("d")}, ord("c") = {ord("c")}') # ord("w") = 119, ord("d") = 100, ord("c") = 99
Output:
www
ord("w") = 119, ord("d") = 100, ord("c") = 99
This one may be a bit counter-intuitive.
In this example max() is taking into account only the first letter of each word
and is calculating max based on ASCII value of a given letter.
In this example, with function ord() we can see that letter 'w' has the biggest value - 119.
Using max() function with key argument provided
my_list = ["www", "devcuriosity", "com"]
my_max = max(my_list, key=len)
print(my_max) # devcuriosity
Output:
devcuriosity
Here everything looks like we would expect, thanks to key-word argument key. We provided len as a function that should
be used to evaluate each argument value. So in this example, we told max() to simply check for the longest word in a list.
Using max() with multiple iterables (without key argument)
my_list_1 = [5, 100, 999]
my_list_2 = [100, 4]
my_max = max(my_list_1, my_list_2)
print(my_max) # [100, 4]
Output:
[100, 4]
Here, we provided not one, but two iterables to max().
And without a specific function, max() checked that 100 is bigger than 4, so it returned [100, 4].
Using max() with multiple iterables (with provided key argument)
my_list_1 = [5, 100, 999]
my_list_2 = [100, 4]
my_max = max(my_list_1, my_list_2, key=lambda x: sum(x))
print(my_max) # [5, 100, 999]
Output:
[5, 100, 999]
In this example, we also provided two iterables, but we also specified key argument with function sum() to evaluate our lists based on their combined value.
Using max() with default on an empty iterable
my_list = []
my_max = max(my_list, default="Empty")
print(my_max) # Empty
Output:
Empty
And here we can see optional key-word argument default in action.
When default is not provided, calling max() on an empty iterable raises a ValueError.
Key Points to remember about max()
-
Flexible with Iterables and Arguments:
max()can find the largest item in an iterable or among multiple arguments. -
Custom Evaluation with key: The
keyargument allows custom criteria for determining the "largest" item. -
Handling Empty Iterables: The
defaultargument is useful for providing a fallback value when an iterable is empty. -
First Occurrence Preference: If multiple items are equal by key criteria,
max()returns the first encountered.
Note: The min() function works in a similar way, finding the smallest item in an iterable or among arguments.
I hope you found this helpful! If you have any feedback, questions or requests, please reach out to me through Contact page!