Sure, there are a few ways to find the maximum and minimum values in a list using Python. Here are a few examples:
Using the max() and min() functions:
The max() and min() functions are built-in functions in Python that can be used to find the maximum and minimum values in a list. The
max() function takes a list as input and returns the largest value in the list. The
min() function takes a list as input and returns the smallest value in the list.
For example, the following code will find the maximum and minimum values in the list
list1:
Python
list1 = [10, 4, 2, 9, 7, 5, 3, 8, 6]
mavalue = max(list1)
min_value = min(list1)
print("The maximum value is", mavalue)
print("The minimum value is", min_value)
The output of the code is:
Code snippet
The maximum value is 10
The minimum value is 2
Using a for loop:
You can also use a for loop to find the maximum and minimum values in a list. The basic idea is to iterate over the list, keeping track of the largest and smallest values that you have seen so far.
For example, the following code will find the maximum and minimum values in the list
list1:
Python
list1 = [10, 4, 2, 9, 7, 5, 3, 8, 6]
mavalue = list1[0]
min_value = list1[0]
for i in range(1, len(list1)):
if list1[i] > mavalue:
mavalue = list1[i]
elif list1[i] < min_value:
min_value = list1[i]
print("The maximum value is", mavalue)
print("The minimum value is", min_value)
The output of the code is the same as the previous example.
Using the enumerate() function:
The enumerate() function in Python can be used to iterate over a list and get the index of each element. This can be useful for finding the maximum and minimum values in a list, because you can use the index to keep track of which element is the largest and smallest so far.
For example, the following code will find the maximum and minimum values in the list
list1:
Python
list1 = [10, 4, 2, 9, 7, 5, 3, 8, 6]
mavalue = list1[0]
min_value = list1[0]
for i, value in enumerate(list1):
if value > mavalue:
mavalue = value
elif value < min_value:
min_value = value
print("The maximum value is", mavalue)
print("The minimum value is", min_value)
The output of the code is the same as the previous two examples.
Markdown for AI
A clean, structured version of this page for AI assistants and LLMs.
We use cookies to ensure you have the best browsing experience on our website. By using our site, you
acknowledge that you have read and understood our
Cookie Policy &
Privacy Policy.
Sure, there are a few ways to find the maximum and minimum values in a list using Python. Here are a few examples:
Using the
max()andmin()functions:The
max()andmin()functions are built-in functions in Python that can be used to find the maximum and minimum values in a list. Themax()function takes a list as input and returns the largest value in the list. Themin()function takes a list as input and returns the smallest value in the list.For example, the following code will find the maximum and minimum values in the list
list1:Python
The output of the code is:
Code snippet
Using a for loop:
You can also use a for loop to find the maximum and minimum values in a list. The basic idea is to iterate over the list, keeping track of the largest and smallest values that you have seen so far.
For example, the following code will find the maximum and minimum values in the list
list1:Python
The output of the code is the same as the previous example.
Using the
enumerate()function:The
enumerate()function in Python can be used to iterate over a list and get the index of each element. This can be useful for finding the maximum and minimum values in a list, because you can use the index to keep track of which element is the largest and smallest so far.For example, the following code will find the maximum and minimum values in the list
list1:Python
The output of the code is the same as the previous two examples.