Find Index of Lowest & Highest Integer in List in Python (3 Examples)

 

Hi! This tutorial will show you how to get the index of the lowest and highest integer in a list in the Python programming language.

Here is an overview:

Let’s get into the Python code!

 

Create Example Python List

Here, we will create the example Python list of integer values the index of whose lowest and highest values we will determine in this tutorial. So, in your Python programming IDE, run the code below to create the example list of integers:

my_list = [3, 1, 5, 2, 4]
 
print(my_list)
# [3, 1, 5, 2, 4]
 
print(type(my_list))
# <class 'list'>

With the example list of integer values created, we will now examine different ways to determine the index of its lowest and highest values.
 

Example 1: Determine Index of Lowest & Highest Integer Using index() Method

In this first example, we will use the buit-in index() method to return the index of the lowest and highest integers in the list:

index_lowest = my_list.index(min(my_list))
 
index_highest = my_list.index(max(my_list))
 
print(index_lowest)
# 1
 
print(index_highest)
# 2

In this example, to determine the index of the lowest value, the min() function is utilized, which returns the minimum value from my_list. The index() method is then applied to my_list with min(my_list) as its argument. This function searches for the first occurrence of the minimum value within the list and returns its index. The obtained index is stored in the variable index_lowest.

Similarly, to find the index of the highest value, the max() function is employed. It retrieves the maximum value from my_list. The index() method is subsequently called with max(my_list) as its argument to identify the index of the maximum value. The resulting index is assigned to the variable index_highest.

Finally, the values of index_lowest and index_highest, which represent the indexes of the lowest and highest values in the list, respectively, are printed using the print() function.
 

Example 2: Determine Index of Lowest & Highest Integer Using for Loop & Conditional if Statement

In this next example, for loop, along with a conditional if statement, is used to get the index of the lowest and highest integers in the list:

# index of lowest value
for i, v in enumerate(my_list):
  if v == min(my_list):
    print(i)
# 1
 
# index of highest value
for j, e in enumerate(my_list):
  if e == max(my_list):
    print(j)
# 2

Here, in the first loop, the enumerate() function is used to obtain both the index i and the value v of each element in my_list. Within the loop, an if statement checks if the current value v is equal to the minimum value found using min(my_list). If the condition is satisfied, the index i is printed.

Similarly, in the second loop, the enumerate() function is used to obtain the index j and the value e of each item in my_list. The loop then checks if the current element value e is equal to the maximum value found using max(my_list). If the condition is true, the index j is printed.

 

Example 3: Determine Index of Lowest & Highest Integer Using List Comprehension

In this last example, we will use list comprehension to return the index of the lowest and highest integers in the list:

indexes = [[i,j] for i, v in enumerate(my_list) if v == min(my_list) for j, e in enumerate(my_list) if e == max(my_list)]
 
print(indexes[0][0])
# 1
 
print(indexes[0][1])
# 2

The list comprehension consists of two parts. The first part [i, j] creates a sub-list containing the indexes of the lowest and highest values. It uses nested loops with enumerate() to iterate over my_list. The outer loop assigns the index i and the corresponding value v from my_list, while the inner loop assigns the index j and the value e.

Within the loops, conditional statements are used to filter the elements. The first condition v == min(my_list) checks if the current value v is equal to the minimum value in my_list, and the second condition e == max(my_list) checks if the current value e is equal to the maximum value in my_list. Only when both conditions are met the corresponding indexes [i, j] are included in the indexes list.

After obtaining the indexes list, the first index is printed using indexes[0][0], and the second index is printed using indexes[0][1]. This assumes that at least one pair of indexes exists in the indexes list. If multiple pairs of indexes are found, only the indexes of the first pair will be printed.

 

So, with that, we have demonstrated, using 3 examples, how to find the index of the lowest and highest integers in a list in Python. I do hope you found this tutorial helpful!

 

Video, Further Resources & Summary

Do you need more explanations on how to find the index of the lowest and highest integers 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 lowest and highest integers 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 how to find the index of the lowest and highest integers in a list in Python. 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.


Top