Find Second Lowest Number in Nested List in Python (2 Examples)
Hi! This short tutorial will show you how to get the second lowest number in a nested list in the Python programming language.
Here is an overview:
Let’s jump into the Python code!
Create Demo Nested List
Therefore, in your Python programming IDE, run the code below to create the nested demo list:
my_list = [[7,6,9],[3,5,8],[4,2,6]] print(my_list) # [[7, 6, 9], [3, 5, 8], [4, 2, 6]] print(type(my_list)) # <class 'list'>
Now that we have created the demo nested list, we will examine two examples of how to determine the second lowest number in the nested list.
Example 1: Get Second Lowest Number in Nested List Using List Comprehension & sorted() Function
new_list = [j for i in my_list for j in i] sorted_list = sorted(set(new_list), reverse=True) second_lowest = [q for q in sorted_list if q != min(sorted_list)] print(min(second_lowest)) # 3
In the above example, first, a new list,
new_list, is created using a nested list comprehension.
This comprehension iterates over each sublist,
my_list, and then iterates over each element,
j, within each sublist. This effectively flattens the nested list into a single-dimensional list,
Next, a sorted list,
sorted_list, is obtained by converting
new_list into a set to remove duplicates.
reverse=True argument is parsed to the
sorted() function to sort the list in descending order.
Then, a new list,
second_lowest, is created using another list comprehension. It iterates over each element,
sorted_list and selects only those elements that are not equal to the minimum value in
This effectively filters out the lowest number, leaving the remaining elements, including the second lowest number, in the list.
Finally, the minimum value of
second_lowest is printed using the print() function, which corresponds to the second lowest number in
Example 2: Get Second Lowest Number in Nested List Using Nested for Loop
In this second example, we will use a nested for loop to get the second lowest number in the nested list:
new_list =  second_lowest =  for i in my_list: for j in i: new_list.append(j) for r in new_list: if r != min(new_list): second_lowest.append(r) print(min(second_lowest)) # 3
Here, we first initialize an empty list,
new_list, and another empty list,
Then, we iterate over each sublist,
my_list, and within that loop, we iterate over each element,
j, within each sublist. For each element
j, it is appended to
new_list using the append() method.
new_list, another loop is initiated, iterating over each element,
Within this loop, we check if the current element,
r, is not equal to the minimum value in
new_list. If this condition is true, it appends
r to the
Finally, after all the iterations, the minimum value of
second_lowest is printed, which corresponds to the second lowest number in the
Video, Further Resources & Summary
Do you need more explanations on how to find the second lowest number in a nested 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 nested 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:
- Find Index in List of Lists in Python (3 Examples)
- Are Dictionaries Faster than Lists in Python?
- Find Union of Two Lists in Python (3 Examples)
- What is a List Node in Python? (2 Examples)
- Introduction to Python Programming
This post has shown, using 2 examples, how to find the second lowest number in a nested list in Python. Your use case will often determine which method to adopt.
I hope you enjoyed this tutorial! In case you have further questions, you may leave a comment below.
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.
Statistics Globe Newsletter