Print Lowest & Highest Integer in List in Python (3 Examples)

 

In this Python tutorial, you’ll learn how to print the lowest and highest integer values in a list that contains both integers and floats.

Here you have a quick overview:

Let’s dive right into the examples!

 

Define Example Data

We will create a sample data list to use as a basis for this tutorial.

my_list = [6, 2.7, 10, 7.9, -3, 0.5]
print(my_list)
# [6, 2.7, 10, 7.9, -3, 0.5]

As you can see, we have created a list object that contains integers and floats. Let’s see how to print the lowest and highest integers in my_list!

 

Example 1: Print Lowest & Highest Integer via for Loop

This first example using for loop could be the easiest to understand, especially if you are a beginner in the Python language. Let’s take a look!

max_int = None
min_int = None

As shown in the previous output, we have first defined the max_int and the min_int variables as ‘None’, which will be used to track the maximum and minimum integers.

for x in my_list:
    if isinstance(x, int):
        if max_int is None or x > max_int:
            max_int = x
        if min_int is None or x < min_int:
            min_int = x
 
print(max_int)
print(min_int)
# 10
# -3

Next, we will use the for loop to iterate through each element in my_list, where isinstance(x, int) checks if the current element is an integer. If the integer found is higher or lower than the one already kept, max_int or min_int are updated, respectively. The final values are printed into the console.

 

Example 2: Print Lowest & Highest Integer in via List Comprehension & max() & min() Functions

Another method to print the minimum and maximum integer values in a list like my_list is to use the max() and min() functions and list comprehension as follows.

int_list = [x for x in my_list if isinstance(x, int)]

First, we created a new list called int_list by using list comprehension. This list will contain only integers from my_list. Now, we can use the max() and min() functions to find the minimum and maximum values in int_list and print these values.

max_int = max(int_list)
min_int = min(int_list)
print(max_int)  
print(min_int)
# 10
# -3

Let’s see one last example!

 

Example 3: Print Lowest & Highest Integer List via filter() & max() & min() Functions

This last example is very similar to Example 2. It uses the filter() function along with the max() and min() functions to print the lowest and highest values in my_list. Check the output below.

int_list = list(filter(lambda x: isinstance(x, int), my_list))

First, we have created a new list called int_list by using the filter() function. This new list contains only integers from my_list. Now, we can use the max() and min() functions to find the minimum and maximum values in int_list. Then we can print these values.

max_int = max(int_list)
min_int = min(int_list)
print(max_int)  
print(min_int)
# 10
# -3

As expected, we have obtained the lowest and highest integer values in my_list. I hope you enjoyed this tutorial!

 

Video, Further Resources & Summary

Do you need more explanations on how to print the maximum and minimum integer values of 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 have a look at the related tutorials on Statistics Globe:

This post has shown how to print the lowest and highest integer in a list of mixed elements in Python. In case you have further questions, you can let me know in the comments.

 

Paula Villasante Soriano Statistician & R Programmer

This page was created in collaboration with Paula Villasante Soriano. Please have a look at Paula’s author page to get further information about her academic background and the other articles she has written for Statistics Globe.

 

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