Python Program to find the second largest number in a list


In this program, you will learn how to find the second largest number in a list in Python.


list1 = [10, 50, 40, 30, 20]

Sorted list: [10, 20, 30, 40, 50]

Second largest number: 40

Example: How to find the second largest number in a list in Python

list1 = [10, 50, 40, 30, 20]
print("Numbers before sorting:", list1)

# sorting implementation start here

for i in range(0, list1.__len__()):
    for j in range(1 + i, list1.__len__()):
        if list1[i] > list1[j]:
            temp = list1[i]
            list1[i] = list1[j]
            list1[j] = temp

print("After Sorting numbers:", list1)
print("Second largest number:", list1[list1.__len__() - 2])

Output:

Numbers before sorting: [10, 50, 40, 30, 20]
After Sorting numbers: [10, 20, 30, 40, 50]
Second largest number: 40