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

 

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

Here is an overview:

Let’s jump into the Python code!

 

Create Demo Python List of Integer Values

Here, we will create the example Python list of integer values whose second largest number we will return.

So, in your Python programming IDE, run the code below to create the demo Python list of integers:

my_list = [10, 5, 8, 20, 3]
 
print(my_list)
# [10, 5, 8, 20, 3]
 
print(type(my_list))
# <class 'list'>

Now that we have created the demo list of integer values, we will examine three different ways to retrieve the second largest number in the list.
 

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

In this first example, we will use the sorted() function to determine the second largest number in the list:

sorted_list = sorted(my_list)
 
second_largest = sorted_list[-2]
 
print(second_largest)
# 10

In this example, the list is first sorted in ascending order. By doing so, the largest number is moved to the end of the sorted list. To find the second largest number, we simply accessed the element at the second-to-last index of the sorted list, sorted_list[-2].

This approach guarantees that the second largest number will be correctly identified regardless of the initial order of the list elements. The sorted() function takes a list as input and returns a new list with the elements sorted in ascending order.

By accessing the element at index -2 of the sorted list, we retrieve the second largest number.

 

Example 2: Get Second Largest Number in List Using max() Function

In this second example, we will use the built-in max() function to get the second largest number in the list:

largest_number = max(my_list)
 
second_largest = max(n for n in my_list if n != largest_number)
 
print(second_largest)
# 10

Here, we utilize the max() function in Python along with a generator expression.

The generator expression in the second code line filters out any numbers in my_list that are equal to the largest_number variable. It essentially creates a new iterable that contains all the numbers from my_list except for the largest number.

The max() function then takes this filtered iterable as its input and returns the largest value from that iterable. Since the largest number has been filtered out, the value returned by max() will be the second largest number in the list.
 

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

In this last example, we will use the set() function to get the second largest number in the list:

my_set = set(my_list)
 
my_set.remove(max(my_list))
 
second_largest = max(my_set)
 
print(second_largest)
# 10

Here, my_list is first converted into a set using the set() function. By doing this, duplicate elements are automatically removed, and we end up with a set that contains unique values from the original list. In this case, my_set becomes {10, 5, 8, 20, 3}.

Next, the maximum value from my_list is obtained using the max() function. This value represents the largest number in the list. It is then removed from my_set using the remove() method. After this step, my_set becomes {10, 5, 8, 3}.

Finally, the second largest number is obtained by applying the max() function to my_set, which returns 10.

 
With that, we have used three examples to demonstrate how to find the second largest number in a list in Python. I do hope you found this tutorial helpful!
 

Video, Further Resources & Summary

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


Top