Find Index of Lowest & Highest Float in List in Python (2 Examples)

 

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

Here is an overview:

Let’s get right into the Python code!

 

Create Demo List of Floats

Here, we will create the demo Python list of floats of which we will get the index of its lowest and highest float.

Therefore, in your favorite Python IDE, run the line of code below to create the demo Python list of float:

my_float = [3.5,6.1,4.8,5.5,2.7]
 
print(my_float)
 
# [3.5, 6.1, 4.8, 5.5, 2.7]
 
 
print(type(my_float))
 
# <class 'list'>

With our demo list of floats created, let us now see 2 examples of how we can determine the index of its lowest and highest floats.
 

Example 1: Get Index of Lowest & Highest Float Using min(), max(), & index() Functions

In this first example, we will use Python’s built-in min() function, max() function, and index() method to get the index of the lowest and highest float in the list:

# find lowest & highest float
lowest_float = min(my_float)
highest_float = max(my_float)
 
 
# get index of lowest & highest float
lowest_float_index = my_float.index(lowest_float)
highest_float_index = my_float.index(highest_float)
 
 
# print result
print(f"The lowest float is {lowest_float}, and its index is {lowest_float_index}")
print(f"The highest float is {highest_float}, and its index is {highest_float_index}")
 
 
# The lowest float is 2.7, and its index is 4
# The highest float is 6.1, and its index is 1

Here, we, first of all, found the lowest and highest float in the list using the min() and max() functions. Next, we used the index() method to find their index, and then we printed the result using Python’s f-string format.
 

Example 2: Get Index of Lowest & Highest Float Using List Comprehension & enumerate() Functions

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

# get index of lowest & highest float
lowest_float_index = [i for i,f in enumerate(my_float) if f == min(my_float)]
highest_float_index = [i for i,f in enumerate(my_float) if f == max(my_float)]
 
 
# print result
print(f"The lowest float is {min(my_float)}, and its index is {lowest_float_index}")
print(f"The highest float is {max(my_float)}, and its index is {highest_float_index}")
 
 
# The lowest float is 2.7, and its index is [4]
# The highest float is 6.1, and its index is [1]

In the above example, the list comprehension iterated through the output of the enumerate() function, which is a list of tuples in the form of (index, element), and then the conditional “if” statement determined when a float in the iteration was exactly equal to the min and max float in the list. When it found an exact match, its index is then returned.

So, with that, we have demonstrated how to find the index of the lowest and highest float in a list in Python. I 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 float 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 float 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 float 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.


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