Find Second Largest Number in Nested List in Python (2 Examples)

 

Hi! This short tutorial will show you how to determine the second largest number in a nested list in the Python programming language.

Here is an overview:

Let’s get right into the Python code!

 

Create Example Nested List

Here, we will create the example nested list of integers whose second largest value we will get in this tutorial.

So, in your Python programming IDE, run the code below to create the example nested list:

my_list = [[5,6,8],[3,7,9],[4,11,2]]
 
print(my_list)
 
# [[5, 6, 8], [3, 7, 9], [4, 11, 2]]
 
print(type(my_list))
 
# <class 'list'>

With the nested list created, let us now examine ways to determine the second largest number in the nested list.
 

Example 1: Get Second Largest Number in Nested List Using Nested for Loop

In this example, we will use a nested for loop to return the second largest number in the nested list:

new_list = []
 
second_largest = []
 
for i in my_list:
  for j in i:
    new_list.append(j)
    for q in new_list:
      if q != max(new_list):
        second_largest.append(q)
 
print(max(second_largest))
 
# 9

Here, we first initialize an empty list called new_list and another empty list called second_largest. Then, we iterate over each sublist i within my_list.

Inside this loop, we further iterate over each element j within sublist i, and append each element j to new_list.

Next, another loop iterates over each element q within new_list, and checks if q is not equal to the maximum value in new_list using the max() function.

If the condition is true, q is appended to second_largest. This loop continues until all elements in new_list have been processed.

Finally, outside the loop, the maximum value from second_largest is printed with the max() function. This value represents the second largest number in my_list.
 

Example 2: Get Second Largest Number in Nested List Using List Comprehension

In this next example, we will use list comprehension to determine the second largest 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_largest = [q for q in sorted_list if q != max(sorted_list)]
 
print(max(second_largest))
 
#9

Here, a new list called new_list is created using list comprehension, which iterates over each sublist i in my_list and further iterates over each element j in sublist i.

This creates a flattened list containing all the elements from the nested list.

Next, new_list is converted to a set to remove any duplicates, and then sorted in descending order using the sorted() function. The resulting sorted list is stored in sorted_list.

A new list comprehension is then used to create a list second_largest, and it iterates over each element q in sorted_list and adds it to the list if it is not equal to the maximum value in sorted_list.

This filters out the largest number and retains all other elements, effectively excluding the maximum value.

Finally, the maximum value from second_largest is printed with the max() function. This value represents the second largest number in my_list.
 

Video, Further Resources & Summary

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

This post has shown, using two examples, how to find the second largest number in a nested list in Python. Your use case will determine which solution to adopt.

I do hope you found this tutorial helpful! 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