Python Program to find Second Largest Number in a List

Write a Python Program to find the Second Largest Number in a List of items using the sort() function, reverse() function, and for loop with a practical example.

Python Program to find the Second Largest Number in a List using sort

This Python program allows users to enter the length. Next, we used For Loop to add numbers to the list. The sort function sorts the List elements in ascending order. Next, we use the Index position to print the Last but one element in a Python List.

NumList = []
Number = int(input("Please enter the Total Number of List Elements: "))
for i in range(1, Number + 1):
    value = int(input("Please enter the Value of %d Element : " %i))
    NumList.append(value)

NumList.sort()

print("The Largest Element in this List is : ", NumList[Number - 2])
Program to find Second Largest Number in a List using sort() function

It is another example of finding the second-largest number in a list using sort() with the reverse argument.

n = [12, 14, 22, 30, 45]

n.sort(reverse=True)
print(n[1])
30

Python Program to find the Second Largest Number in a List using reverse function

The sort() function in this program will sort the elements in Ascending order. Next, we used the Python reverse function to reverse the items. Lastly, we used index position 1 to print the second element in a list. If you want the largest, use the index position 0.

NumList = []
Number = int(input("Please enter the Total Number of List Elements: "))
for i in range(1, Number + 1):
    value = int(input("Please enter the Value of %d Element : " %i))
    NumList.append(value)

NumList.sort()
NumList.reverse()
print("The Second Largest Element in this List is : ", NumList[1])
Second Largest Number in a List using reverse

Python Program to find the Second Largest Number in a List using for loop

In this Python program, we are not using any built-in function, such as the Python sort or the list reverse function. For this, we use Python For Loop.

NumList = []
Number = int(input("Please enter the Total Number of Elements: "))

for i in range(1, Number + 1):
    value = int(input("Please enter the Value of %d Element : " %i))
    NumList.append(value)

first = second = NumList[0]
for j in range(1, Number):
    if(NumList[j] > first):
        second = first
        first = NumList[j]
    elif(NumList[j] > second and NumList[j] < first):
        second = NumList[j]
        

print("The Largest Element in this List is : ", first)
print("The Second Largest Element in this List is : ", second)
Program to find Second Largest Number in a List using for loop

From the above screenshot, you can observe that the User inserted values are NumList[4] = {55, 57, 22, 3}
first = second = NumList[0] = 55

First Iteration – for 1 in range(1, 4) – Condition is true. So, it executes the Python If statement inside the loop until the condition fails.
if(NumList[j] > first) inside the for loop is True because (57 > 55)
second = first = 55
first = NumList[1] = 57

Second Iteration: for 2 in range(1, 4) – Condition is true.
If (NumList[2] > first) = (22 > 57) – Condition is False. So, it enters into an Python elif statement
elif(NumList[2] > second and NumList[2] < first)
elif(22 > 55 and 22 < 57) – Condition is False

Third Iteration: for 3 in range(1, 4) – Condition is true.
If (3 > 57) – Condition is False
elif(3 > 55 and 3 < 57) – Condition is False

Fourth Iteration: for 4 in range(1, 4) – Condition is False. So, it exits from the loop.

Second Largest List Number using heapq.nlargest()

We can use the nlargest function from the heapq module to extract the Nth (2nd) list item. We must provide N (2) and the list name as two arguments, and nlargest() returns a list of the two largest values. Apply the index to get the second-largest list item.

from heapq import nlargest
n = [12, 14, 22, 30, 45]

sec = nlargest(2, n)
print(sec[1])
30

Also Read