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

Xiith is created for educational, experimental, and schooling purpose. Examples on Xiith are made easier to make a better or basic understanding. Tutorials, testimonials, and examples are continuously checked to avoid delusion, but we cannot take complete responsibility of all programs on Xiith. When you are operating this site, you have to agree to read and accept our terms of use, cookie and privacy policy.Copyright © 2020 by Xiith. All Rights Reserved. About us.