Skip to content

Instantly share code, notes, and snippets.

@hossainlab
Last active August 22, 2022 15:09
Show Gist options
  • Save hossainlab/c0ce2d52d6c5b5d4b126b45f4956efb2 to your computer and use it in GitHub Desktop.
Save hossainlab/c0ce2d52d6c5b5d4b126b45f4956efb2 to your computer and use it in GitHub Desktop.
How To Find the Second Smallest Number in a List

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

Problem Description

The program takes a list and prints the second smallest 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 smallest value of this list: ",li[1])

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 first element of the list is printed which is also the smallest 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 smallest value of this list: ",li[1])
Enter the number of elements: 4
Enter the elements: 50
Enter the elements: 45
Enter the elements: 36
Enter the elements: 12
The sorted list:  [12, 36, 45, 50]
The second smallest value of this list:  36

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 smallest value of this list: ",li[1])
Enter the number of elements: 5
Enter the elements: 10
Enter the elements: 40
Enter the elements: 70
Enter the elements: 100
Enter the elements: 130
The sorted list:  [10, 40, 70, 100, 130]
The second smallest value of this list:  40

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 smallest value of this list: ",li[1])
Enter the number of elements: 10
Enter the elements: -3
Enter the elements: -2
Enter the elements: -1
Enter the elements: 3
Enter the elements: 2
Enter the elements: 1
Enter the elements: 0
Enter the elements: 4
Enter the elements: 3
Enter the elements: 4
The sorted list:  [-3, -2, -1, 0, 1, 2, 3, 3, 4, 4]
The second smallest value of this list:  -2
@tapassaha-01
Copy link

test case- [1,1,2,3,6]

@toghani
Copy link

toghani commented Aug 29, 2021

This code will not work if there are two (or more!) small equal numbers
EX : [2,2,2,2,4,6,7]
output for your code is 2 ! but 4 is true answer

@AlifTech03
Copy link

u should make it into set while u got duplicate no in your list then this formula work otherwise it wouldnt

@Aditya35639678
Copy link

Will this work..?

b = []
c = []
n = int(input())
for i in range(n):
a = []
ipc = input()
a.append(ipc)
ipi = eval(input())
a.append(ipi)
c.append(ipi)
b.append(a)
c.sort()
e = []
mc = min(c)
smc = 0
for w in c:
if mc == w:
pass
else:
smc = w
break
for j in range(n):
if smc == b[j][1]:
e.append(b[j][0])
e.sort()
for k in e:
print(k)

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