Find Min & Max Value in Linked List in Python (2 Examples)

 

Hi! This tutorial will demonstrate how to determine the index of the lowest and highest values in a list in the Python programming language.

Here is a quick overview:

Let’s get coding!

 

Create Demo Integer List

We will, here, create the demo Python list of integers that we will use in the examples in this tutorial. Therefore, in your preferred Python IDE, run the code below to create the demo integer list:

int_list = [2,5,1,8,3,4,6,0]
 
print(int_list)
 
# [2, 5, 1, 8, 3, 4, 6, 0]
 
print(type(int_list))
 
# <class 'list'>

With the demo list created, we will now see examples of how to return the indexes of the smallest and largest values in the list.
 

Example 1: Get Index of Lowest & Highest Values Using index() Function

In this first example, we will, first all, use Python’s min() function and max() function to get the minimum and maximum values in the list; then, we will use the index() function to return their respective indexes:

# get lowest & highest values
min_val = min(int_list)
max_val = max(int_list)
 
 
# get indexes of lowest & highest values
indx_min = int_list.index(min_val)
indx_max = int_list.index(max_val)
 
 
# print results
print(f"The min value is {min_val} & its index is {indx_min}")
print(f"The max value is {max_val} & its index is {indx_max}")
 
 
# The min value is 0 & its index is 7
# The max value is 8 & its index is 3

In the example above, we followed 3 simple steps to finding the indexes of the min and max values in the list. The first step was to get the min and max values in the list. The next step was to determine their index positions. The last step was to print out the results. For that, we made use of Python’s f-strings to embed our results inside strings.
 

Example 2: Get Index of Lowest & Highest Values Using List Comprehension & enumerate() Function

In this example, we will use list comprehension, and the enumerate() function to get the indexes of the lowest and highest values in the list:

# get index of lowest value in list
min_indx = [i for i,j in enumerate(int_list) if j == min(int_list)]
 
 
# get index of highest value in list
max_indx = [i for i,j in enumerate(int_list) if j == max(int_list)]
 
 
# print results
print(f"The min value is {min(int_list)} & its index is {min_indx}")
print(f"The max value is {max(int_list)} & its index is {max_indx}")
 
 
# The min value is 0 & its index is [7]
# The max value is 8 & its index is [3]

In the above example, we used list comprehension to iterate through the output of the enumerate() function, which is a list of tuples containing the values in the list and their respective index positions.

The variable i returns the index position, and the variable j returns the value. Therefore, we used a conditional statement to return the index number of a value in the list when it is exactly equal (denoted by double equals sign ==) to the minimum value or the maximum value.

So, with these 2 simple examples, we have demonstrated how to find the index of the min and max values 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 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 lists tutorials on Statistics Globe, starting with these ones:

This post has shown how to find the index of the min and max values in a Python list. 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