Python How To Find Max In List
Today we are going to show you how to use Python to find the max value in a list. This is really easy to do in Python and doesn’t require you to implement an algorithm or do anything manually. You really just need to call the max() function. You can do this with a list of numerical values and strings.
Here is an example showing how you can find the max value in a list using Python:
a = [ 1, 78, 2, 2, 4, 5 ]
max(a)
The output will look like this:
78
Here is another example:
a = [ 1, 2, 2, 4, 5, 3, 1 ]
max(a)
This is what the output would look like:
5
Python Find Max in List of Strings
You can still find the max value in a list even if it doesn’t contain all numerical values. This works fine with a list of strings. Each string would have a numerical value based on alphabetical order.
Here is an example showing you can find the max value in a list of strings.
a = [ "z", "asdf", "a", "b", "c" ]
print(max(a))
This would output:
z
Here is another example:
a = [ "asdf", "a", "b", "c" ]
print(max(a))
This would output:
c