The max() function in Python returns the largest element from an iterable (such as a list, tuple, or set) or the largest among two or more arguments.
Let’s find the largest number in a list.
data_list = [6, 5, 3, 8, 10, 2, 4] max_number = max(data_list) print(max_number) # Output: 10
Syntax
# With a single iterable argument max(iterable, key=None, default=None) # With multiple arguments max(arg1, arg2, *args[, key])
It has two syntaxes, which totally depend on the type of argument you are providing.
Parameters
Argument | Description |
iterable | It represents an iterable object (e.g., list, tuple, string, set, dictionary keys). |
*arg1, arg2, args (required for the second form) | If you are passing multiple arguments, these are helpful.
They represent two or more individual arguments that can be compared directly. |
key (optional, default=None) | It is a callable function that accepts one argument and returns a value used for comparison. |
default (optional, default=None; iterable form only) | It represents the value returned if the iterable is empty. |
Multiple arguments
It compares individual values directly.
maximum_value = max(10, 20, 150, 30) print(maximum_value) # Output: 150
We have not passed any iterable. Instead, we passed four individual numbers, and the max() function returns the maximum value among them. In our case, it is 150.
Strings (Lexicographical Comparison)
What about strings? How will it calculate the maximum values? Strings are compared character by character using Unicode values. Therefore, the character with the highest Unicode value will be returned as the maximum.
cars = ["bmw", "lambo", "toyota", "jaguar"] max_car = max(cars) print(max_car) # Output: toyota
Here is the table for the ASCII value of each first character of the string:
Character | ASCII Value (Unicode points) |
bmw => “b” | 98 |
lambo => “l” | 108 |
toyota => “t” | 116 |
jaguar => “j” | 106 |
In the above code, the character “t” has the highest unicode value, which is why “toyota” is the maximum value.
What if there are toyota and tata cars? Let’s find out.
cars = ["bmw", "tata", "jaguar", "toyota"] max_car = max(cars) print(max_car) # Output: toyota
In the above code, the first character of both values is the same, which is “t”, and its ASCII value is 116.
So, which value is the maximum in that case? Well, in that case, we need to check the second character in both strings.
The second character of both strings is “a” and “o”. Here are the ASCII values of the second characters:
Second Character | ASCII Value (Unicode points) |
tata => “a” | 97 |
toyota => “o” | 111 |
As you can see, the toyota has the max value because the second character is “o” and hence it is the output.
Key Function
What if you want to compare the string by length instead of lexicographically? In that case, we can use the “key” argument and pass the custom function that checks for the length of each string and returns a string with the maximum length.
words = ["cat", "elephant", "dog"] print(max(words, key=len)) # Output: elephant
As you can see, “elephant” has a maximum length of 8. So, we have that as an output. The len() function returns the length of each string in the list.
Handling empty iterables with default
What if the list you pass is empty? In that case, there is an argument called “default” that you can use, which will return the default value if the list is empty or an iterable is empty. It is a fail-safe approach that prevents any error.
empty_list = [] print(max(empty_list, default="No Data")) # Output: No Data
Here, the string “No Data” is considered the maximum value because the input list is empty.