Find Second Lowest Number in List in Python (3 Examples)

 

Hi! This tutorial will show you how to get the second lowest number in a list in the Python programming language.

Here is an overview:

Let’s get right into the Python code!

 

Create Example Python List

Here, we will create the example Python list of integer values whose second lowest number we will retrieve in this tutorial. Therefore, in your Python programming IDE, run the code below to create the example list of integers:

my_list = [15,9,10,25,6]
 
print(my_list)
# [15, 9, 10, 25, 6]
 
print(type(my_list))
# <class 'list'>

Having created the example Python list of integer values, we will now examine three ways to return the second lowest number in the list.
 

Example 1: Get Second Lowest Number in List Using sorted() Function

In this first example, we will use the built-in sorted() function to obtain the second lowest number in the list:

sorted_list = sorted(my_list)
 
second_lowest = sorted_list[1]
 
print(second_lowest)
# 9

In this example, a new list called sorted_list is created by sorting the elements of my_list in ascending order.

Afterward, the second element is retrieved from the sorted list using index notation and assigned it to the variable second_lowest.

In other words, second_lowest will store the value of the second smallest number in my_list once it has been sorted.

 

Example 2: Get Second Lowest Number in List Using min() Function

In this next example, we will use the min() function to get the second lowest number in the list:

lowest_number = min(my_list)
 
second_lowest = min(n for n in my_list if n != lowest_number)
 
print(second_lowest)
# 9

Here, we first calculate the lowest number in the list my_list by using the min() function and assign it to the variable lowest_number.

Then, we use a generator expression to iterate over the elements of my_list and filter out any elements that are equal to the lowest number.

The min() function is again applied to this filtered sequence to find the second lowest number, which is then assigned to the variable second_lowest.

Essentially, we find the smallest number in the list and then find the smallest number that is different from the smallest number itself, giving us the second lowest number in my_list.

 

Example 3: Get Second Lowest Number in List Using set() Function

In this final example, we will use the set() function to determine the second lowest number in the list:

my_set = set(my_list)
 
my_set.remove(min(my_list))
 
second_lowest = min(my_set)
 
print(second_lowest)
# 9

In this example, we use a set, which is an unordered collection of unique elements, so any duplicate values in my_list are automatically removed.

The lowest number in my_list is found using the min() function, and then it is removed from my_set using the remove() method.

After removing the lowest number, the remaining numbers in my_set are still unique. The second lowest number is then obtained by applying the min() function to my_set, and its value is assigned to the variable second_lowest.

Essentially, we create a set from the list, remove the lowest number, and find the minimum value from the remaining unique numbers in the set, giving us the second lowest number in the original list.

 
So, using these three examples, we have demonstrated how to find the second lowest number in a list in Python. I sure hope you found this tutorial helpful!
 

Video, Further Resources & Summary

Do you need more explanations on how to find the second lowest number 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 second lowest number 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 second lowest number 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