To get the maximum of a list of numbers or strings in Python, we can use the Python max() function. You can use the max() function in Python to get the maximum value of numbers in a list, strings in a list, and values in a dictionary.
list_of_numbers = [10,32,98,38,47,34]
print(max(list_of_numbers))
#Output
98
There are many powerful built-in functions in the Python language which allow us to perform basic or complex tasks easily.
One such task is to find the maximum value of a collection of numbers or strings.
We can use the Python max() function to find the maximum, or biggest, value in an iterable (list, dictionary, set, etc.) made up of numbers or strings.
Let’s say we have the following list of numbers in Python. We can use the Python built-in max() function to get the maximum value of the list in the following way:
list_of_numbers = [10,32,98,38,47,34]
print(max(list_of_numbers))
#Output
98
We can also call the max() function with multiple arguments. In this case, max() returns the biggest value of the arguments.
print(max(10,32,98,38,47,34))
#Output
98
For a collection of strings, the biggest value is determined after sorting alphabetically.
print(max("Mike","Alex","Patricia"))
#Output
Patricia
If you instead want to find the minimum value of an iterable, you can use the Python min() function.
If you are using pandas in Python and want to get the maximum value of a column or DataFrame, you can use the pandas max() function.
How to Find Maximum of Two Numbers Using Python max() Function
In Python, we can easily find the maximum of two numbers using the max() function.
Below are a few examples of getting the max value between two numbers using max().
print(max(10,32))
print(max(-30,4))
print(max(40,-21))
print(max(8,-22))
print(max(98,99))
#Output
32
4
40
8
99
How to Find Biggest Value in List of Numbers Using Python max() Function
We can use the Python max() function to get the biggest value in a list of numbers easily. To find the max value of a list, we just pass the list to max().
Let’s say we have the following list of numbers.
list_of_numbers = [10,32,98,38,47,34]
To get the maximum value of a list of numbers in Python, we use the max() function.
list_of_numbers = [10,32,98,38,47,34]
print(max(list_of_numbers))
#Output
98
How to Get Maximum Value in List of Strings Using Python max() Function
We can use the Python max() function to get the biggest value in a list of strings easily. To find the max value of a list, we just pass the list to max().
The biggest value from a list of strings is the first string in alphabetical order from Z to A, meaning the string that starts with a letter closest to Z will be the biggest. Also, note that uppercase letters come before lowercase letters.
Let’s say we have the following list of strings.
list_of_strings = ["Low","High","Better","Worse"]
To get the maximum value of a list of strings in Python, we use the max() function.
list_of_strings = ["Low","High","Better","Worse"]
print(max(list_of_strings))
#Output
Worse
How to Find Maximum Value in Dictionary Using Python max() Function
With the Python max() function, we can find the maximum value in a dictionary.
Let’s say we have the following dictionary.
dict = { "Alex": -3, "John":5, "Tammy":-10, "Jim":4 }
To get the max value in a dictionary, we pass the dictionary to max() as shown in the following Python code.
dict = { "Alex": -3, "John":5, "Tammy":-10, "Jim":4 }
print(max(dict))
#Output:
4
We can also get the maximum value of the keys in a dictionary. To do so, we need to pass a lambda expression for max() as the “key”.
Below is how to find the max value of the keys in a dictionary in Python.
dict = { "Alex": -3, "John":5, "Tammy":-10, "Jim":4 }
print(max(dict, key = lambda k: dict[k]))
#Output:
Tammy
Hopefully this article has been useful for you to learn how to use the Python max() function to get the maximum value of a list of numbers or strings.