Skip to content

Instantly share code, notes, and snippets.

@hossainlab
Created August 18, 2018 17:46
Show Gist options
  • Save hossainlab/1d03970bd1aacec8b58e6b9b171b5328 to your computer and use it in GitHub Desktop.
Save hossainlab/1d03970bd1aacec8b58e6b9b171b5328 to your computer and use it in GitHub Desktop.
How To Find the Second Largest Number in a List

Python Program to Find the Second Largest Number in a List

This is a Python Program to find the Second largest number in a list.

Problem Description

The program takes a list and prints the Second largest number in the list.

Problem Solution

  1. Take in the number of elements and store it in a variable.
  2. Take in the elements of the list one by one.
  3. Sort the list in ascending order.
  4. Print the last element of the list.
  5. Exit.

Source Code

li = [] 
n = int(input("Enter the number of elements: ")) 
for i in range(1, n+1): 
    elem = int(input("Enter the elements: "))
    li.append(elem) 
li.sort() 
print("The sorted List: ",li) 
print("The second largest value of sorted list: ",li[n-2]) 

Program Explanation

  1. User must enter the number of elements and store it in a variable.
  2. User must then enter the elements of the list one by one using a for loop and store it in a list.
  3. The list should then be sorted.
  4. Then the last element of the list is printed which is also the largest element of the list.

Test Case-1

li = [] 
n = int(input("Enter the number of elements: ")) 
for i in range(1, n+1): 
    elem = int(input("Enter the elements: "))
    li.append(elem) 
li.sort() 
print("The sorted List: ",li) 
print("The second largest value of sorted list: ",li[n-2])
Enter the number of elements: 4
Enter the elements: 50
Enter the elements: 54
Enter the elements: 60
Enter the elements: 63
The sorted List:  [50, 54, 60, 63]
The second largest value of sorted list:  60

Test Case-2

li = [] 
n = int(input("Enter the number of elements: ")) 
for i in range(1, n+1): 
    elem = int(input("Enter the elements: "))
    li.append(elem) 
li.sort() 
print("The sorted List: ",li) 
print("The second largest value of sorted list: ",li[n-2])
Enter the number of elements: 5
Enter the elements: -10
Enter the elements: -70
Enter the elements: -3
Enter the elements: 0
Enter the elements: -4
The sorted List:  [-70, -10, -4, -3, 0]
The second largest value of sorted list:  -3

Test Case-3

li = [] 
n = int(input("Enter the number of elements: ")) 
for i in range(1, n+1): 
    elem = int(input("Enter the elements: "))
    li.append(elem) 
li.sort() 
print("The sorted List: ",li) 
print("The second largest value of sorted list: ",li[n-2])
Enter the number of elements: 4
Enter the elements: 10
Enter the elements: -40
Enter the elements: -3
Enter the elements: 80
The sorted List:  [-40, -3, 10, 80]
The second largest value of sorted list:  10

Jubayer Hossain

Please share your tips or tricks for solving this problem in a better way! Happy Coding!

@udoyen
Copy link

udoyen commented Mar 16, 2023

Hello sir, can you explain the last line in your code 'li[n-2])' and how it picks the second largest number

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment