# 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

We will here create the demo nested Python list of integers whose second lowest number we will determine in this tutorial.

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

In this first example, we will use list comprehension and the sorted() function to return the second smallest number in the nested list:

```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, `i`, in `my_list`, and then iterates over each element, `j`, within each sublist. This effectively flattens the nested list into a single-dimensional list, `new_list`.

Next, a sorted list, `sorted_list`, is obtained by converting `new_list` into a set to remove duplicates.

The `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, `q`, in `sorted_list` and selects only those elements that are not equal to the minimum value in `sorted_list`.

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 `my_list`.

## 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, `second_lowest`.

Then, we iterate over each sublist, `i`, in `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.

After appending `j` to `new_list`, another loop is initiated, iterating over each element, `r`, in `new_list`.

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 `second_lowest` list.

Finally, after all the iterations, the minimum value of `second_lowest` is printed, which corresponds to the second lowest number in the `my_list`.

## 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.

Furthermore, I encourage you to check out other interesting Python list tutorials on Statistics Globe, starting with these ones:

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.

Fin

Subscribe to the Statistics Globe Newsletter