Find Index of Min & Max Value in List in Python (3 Examples)

 

Hi! This tutorial will show you how to get the indexes of the min and max values in a list in the Python programming language.

Here is an overview:

Let’s get into the Python code!

 

Create Example List

Here, we will create the example Python list of integers, we will determine the indexes of the min and max values in this tutorial.

So, in your preferred Python coding IDE, run the code below to create the example list:

my_list = [5,8,6,10,3,7]
 
print(my_list)
 
# [5, 8, 6, 10, 3, 7]
 
print(type(my_list))
 
# <class 'list'>

With the example list created, we will now see three ways to return the indexes of the min and max values in the list.
 

Example 1: Get Index of Min & Max Value in List Using index() Method

In this first example, we will use the built-in index() method to determine the indexes of the min and max values in the list:

try:
  print(my_list.index(min(my_list)))
  print(my_list.index(max(my_list)))
except ValueError:
  print("An error occurred")
 
# 4
# 3

In the example above, we use the try-except block to perform two operations on the list.

First, the min() function is used to find the smallest value in the list, and its index is retrieved using the index() method. This index is then printed to the console using the print() function.

Next, the max() function is used to find the largest value in the list, and its index is also retrieved using the index() method, which is then printed to the console.

If either of these operations encounters a ValueError (indicating that the minimum or maximum value is not found in the list), the code jumps to the except block and prints the message “An error occurred.”
 

Example 2: Get Index of Min & Max Value in List Using for Loop

In this next example, we will use a for loop to iterate through the list and return the indexes of the min and max values:

# min value index
for index,element in enumerate(my_list):
  if element == min(my_list):
    print(index)
 
# 4
 
# max value index
for index,value in enumerate(my_list):
  if value == max(my_list):
    print(index)
 
# 3

Here, a loop uses the enumerate() function to iterate over the elements of the list along with their corresponding indices.

Inside the loop, we check if the current element is equal to the minimum value in the list, which is determined using the min() function. If the condition is true, the indexes of that element is printed to the console.

After that, we run another loop using the enumerate() function to iterate over the elements of the list and their indices.

This time, we check if the current value is equal to the maximum value in the list, obtained using the max() function. If the condition is true, the index of that element is printed to the console.

 

Example 3: Get Index of Min & Max Value in List Using List Comprehension

In this last example, we will use list comprehension to determine the indexes of the min and max values in the list:

min_index = [index for index, element in enumerate(my_list) if element == min(my_list)][0]
 
print(min_index)
 
# 4
 
max_index = [index for index, value in enumerate(my_list) if value == max(my_list)][0]
 
print(max_index)
 
# 3

In the above example, we use a list comprehension to iterate over the indices and elements of the list obtained using the enumerate() function.

We check if each element is equal to the minimum value in the list, determined by the min() function. If the condition is true, the index is added to min_index.

Since there may be multiple indices corresponding to the minimum value, the first index is retrieved by accessing the element at index 0 of min_index. Finally, the minimum index is printed to the console.

Next, we follow a similar process to find the index of the maximum value in the list. We use another list comprehension to iterate over the indices and values of the list.

We again check if each value is equal to the maximum value in the list, obtained using the max() function.

The indices meeting this condition are added to max_index. The first index is retrieved from max_index by accessing the element at index 0. Lastly, the maximum index is printed to the console.
 

Video, Further Resources & Summary

Do you need more explanations on how to find the index of the min and max values in a list in Python? Then you should have a look at the following YouTube video of the Statistics Globe YouTube channel.

In the video, we explain in some more detail how to find the index of the min and max values in a list in Python.

 

The YouTube video will be added soon.

 

Furthermore, I encourage you to check out other interesting Python list tutorials on Statistics Globe, starting with these ones:

This post has shown, using three examples, how to find the index of the min and max values in a list in Python. Your use case will determine which solution to adopt.

I do hope you found this tutorial helpful! In case you have further questions, you may leave a comment below.

 

R & Python Expert Ifeanyi Idiaye

This page was created in collaboration with Ifeanyi Idiaye. You might check out Ifeanyi’s personal author page to read more about his academic background and the other articles he has written for the Statistics Globe website.

 

Subscribe to the Statistics Globe Newsletter

Get regular updates on the latest tutorials, offers & news at Statistics Globe.
I hate spam & you may opt out anytime: Privacy Policy.


Leave a Reply

Your email address will not be published. Required fields are marked *

Fill out this field
Fill out this field
Please enter a valid email address.

Top