Print Lowest & Highest Float in List in Python (3 Examples)
In this Python article, you’ll learn how to print the lowest and highest float in a list.
The content in this article is structured as follows:
Let’s jump into the code!
Create Sample Data
As a first step in this article, we will create a sample list as follows.
my_list = [4, 2.5, 7.3, 1, 9.1, 3, 8.7, 6.4, 0.5] print(my_list) # [4, 2.5, 7.3, 1, 9.1, 3, 8.7, 6.4, 0.5]
We have constructed a list object containing 3 integers (e.g., 4) and 6 floats (e.g., 7.3). How to print the maximum and minimum float values in my_list
? Let’s take a look!
Example 1: Use max() & min() Functions Along With List Comprehension to Print Maximum & Minimum Float in List
In this first example, we will use the max() and min() functions along with a list comprehension to select the float values.
floats = [num for num in my_list if isinstance(num, float)] print("Lowest float value:", min(floats)) print("Highest float value:", max(floats)) # Lowest float value: 0.5 # Highest float value: 9.1
As you can see in the previous Python output, we created a new list called floats
. In this list, the comprehension iterates over each element num
in my_list
and includes it in the floats
list only if isinstance(num, float)
returns True, meaning that num
is a float. This filters out any non-float elements in the original list.
Finally, the lowest and highest values are found by the min() and the max() functions, respectively, and printed into the console. Easy!
Example 2: Use for Loop to Print Maximum & Minimum Float in List
This method shows how to use a for loop to print the lowest and highest float values in a list.
max_float = None min_float = None for num in my_list: if isinstance(num, float): if max_float is None or num > max_float: max_float = num if min_float is None or num < min_float: min_float = num print("Lowest float value:", min_float) print("Highest float value:", max_float) # Lowest float value: 0.5 # Highest float value: 9.1
As shown, we have first initialized two variables called max_float
and min_float
to None
. Then the for loop iterates over each element num
in my_list
and checks if num
is a float using isinstance(num, float)
.
If so, the code checks if it is greater than the current max_float
value or less than the current min_float
value, and updates max_float
and min_float
if the conditions are met.
Example 3: Use sorted() Function & for Loop to Print Maximum & Minimum Float in List
This last example uses the sorted() function to create a sorted copy of my_list
.
sorted_list = sorted(my_list) for num in sorted_list: if isinstance(num, float): print("Lowest float value:", num) break for num in reversed(sorted_list): if isinstance(num, float): print("Highest float value:", num) break # Lowest float value: 0.5 # Highest float value: 9.1
As you can see, first, a for loop iterates over the sorted list and uses isinstance(num, float)
to check if each element is a float. If a float value is found, it’s printed, and then the code breaks out of the loop using the break
statement.
Given that the list is sorted in ascending order, the first float value will be the lowest. Next, a for loop iterates over the reversed sorted list and follows the same process to print the maximum float value.
That’s how we get our maximum and minimum float values in my_list
in three different ways :). I hope you liked it!
Video, Further Resources & Summary
Do you need more explanations on how to print the maximum and minimum float values in a list in Python? Then you should have a look at the following YouTube video of the Statistics Globe YouTube channel.
The YouTube video will be added soon.
Furthermore, you could take a look at the related tutorials on Statistics Globe:
- Count Min & Max Occurrences in List in Python
- Print List without Square Brackets in Python
- Python Overview
- Find Min & Max Value in Linked List in Python
This post has shown how to print the lowest and highest float values in a list in Python. Please let me know in the comments section below if you have additional questions.
This page was created in collaboration with Paula Villasante Soriano. Please have a look at Paula’s author page to get more information about her academic background and the other articles she has written for Statistics Globe.
Statistics Globe Newsletter