How do you find the maximum and minimum values in a list in Python?
How do you find the maximum and minimum values in a list in Python?
290
26-Jun-2023
Aryan Kumar
27-Jun-2023There are two built-in functions in Python that you can use to find the maximum and minimum values in a list:
max(): Returns the largest value in a list.min(): Returns the smallest value in a list.For example, the following code finds the maximum and minimum values in the list
list1:Python
This will print the following output:
Code snippet
The
max()andmin()functions can also be used to find the maximum and minimum values in a list of strings. For example, the following code finds the maximum and minimum values in the listlist2:Python
This will print the following output:
Code snippet
In this case, the
max()function compares the ASCII values of the strings in the list. The ASCII value oforangeis 79, which is the highest in the list. The ASCII value ofkiwiis 75, which is the lowest in the list.