How to Discover the 2nd Largest Number in List in Python


In this example we will show how to select the second largest number in the list with Python.

Source Code

#! /usr/bin/env python3
# -*- coding: utf-8 -*-

n = input("Please input a list of numbers separated by SPACE: ")
a = n.split()
b = []
for i in a:
    b.append(int(i))
b.sort(reverse=True)
print("The second largest number is:", b[1])

Output:

Please input a list of numbers separated by SPACE: 12 18 13 26 15
The second largest number is: 18

Please input a list of numbers separated by SPACE: 89 78 106 65 23 98
The second largest number is: 98

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments