• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Programming Expert

Solving All of Your Programming Headaches

  • HTML
  • JavaScript
  • jQuery
  • PHP
  • Python
  • SAS
  • Ruby
  • About
You are here: Home / Python / Using Python to Find Second Smallest Value in List

Using Python to Find Second Smallest Value in List

February 24, 2022 Leave a Comment

In Python, we can find the second smallest value in a list easily using a simple function and a for loop.

list_of_numbers = [2, 1, 9, 8, 6, 3, 1, 0, 4, 5]
  
def findSecondSmallest(lst):
    firstSmallest = min(lst[0],lst[1])
    secondSmallest = max(lst[0],lst[1])
    for i in range(2,len(lst)):
        if lst[i] < firstSmallest:
            secondSmallest = firstSmallest 
            firstSmallest = lst[i]
        elif lst[i] < secondSmallest and firstSmallest < lst[i]:
            secondSmallest = lst[i]
    return secondSmallest 

print(findSecondSmallest(list_of_numbers))

#Output:
1

We can also use the sort() function and get the second element of the list after sorting.

list_of_numbers = [2, 1, 9, 8, 6, 3, 1, 0, 4, 5]
  
def findSecondSmallest(lst):
    temp_lst = lst.copy()
    temp_lst.sort()
    return temp_lst[1]

print(findSecondSmallest(list_of_numbers))

#Output:
1

You can also use the remove() function to remove the minimum value found by the min() function, and then use the min() function again.

list_of_numbers = [2, 1, 9, 8, 6, 3, 1, 0, 4, 5]
  
def findSecondSmallest(lst):
    temp_lst = lst.copy()
    firstSmallest = min(temp_lst)
    temp_lst.remove(firstSmallest)
    return max(temp_lst)

print(findSecondSmallest(list_of_numbers))

#Output:
1

There are many powerful built-in functions in the Python language which allow us to perform basic or complex tasks easily.

One such task is to find the minimum of a list of numbers.

However, what if you want to find the second smallest number in a list?

We can easily find the second smallest value in a list using a loop in Python.

To find the second smallest value in a list, we just need to keep track of the smallest number and the second smallest number, and loop over the list of numbers.

Below is an example of a Python function which gets the second smallest value in a list of numbers.

list_of_numbers = [2, 1, 9, 8, 6, 3, 1, 0, 4, 5]
  
def findSecondSmallest(lst):
    firstSmallest = min(lst[0],lst[1])
    secondSmallest = max(lst[0],lst[1])
    for i in range(2,len(lst)):
        if lst[i] < firstSmallest:
            secondSmallest = firstSmallest 
            firstSmallest = lst[i]
        elif lst[i] < secondSmallest and firstSmallest < lst[i]:
            secondSmallest = lst[i]
    return secondSmallest 

print(findSecondSmallest(list_of_numbers))

#Output:
1

Using sort() to Find Second Smallest Value in List with Python

Another way that we can find the second smallest number in a list of numbers is with sorting.

We can sort a list in ascending order, and we then know that the smallest number is the first number of the sorted list, the second smallest number is the second number, and so on.

We can access the second element of a list by accessing the ‘1’ position.

Below is an example in Python of how to get the second smallest value in a list using sort().

list_of_numbers = [2, 1, 9, 8, 6, 3, 1, 0, 4, 5]
  
def findSecondSmallest(lst):
    temp_lst = lst.copy()
    temp_lst.sort()
    return temp_lst[1]

print(findSecondSmallest(list_of_numbers))

#Output:
1

Using max() and remove() to Get Second Smallest Value of List in Python

One final way you can get the second smallest value of a list in Python is to remove the minimum value from the list, and then find the minimum of the modified list.

First, we need to use the min() function to get the minimum value of the list. Then we use the list remove() function to remove the minimum value. We can then use the min() function again to get the second smallest number.

Below is an example in Python of how to get the second smallest value of a list using min() and remove().

list_of_numbers = [2, 1, 9, 8, 6, 3, 1, 0, 4, 5]
  
def findSecondSmallest(lst):
    temp_lst = lst.copy()
    firstSmallest = min(temp_lst)
    temp_lst.remove(firstSmallest)
    return max(temp_lst)

print(findSecondSmallest(list_of_numbers))

#Output:
1

How to Find Second Largest Value in a List Using Python

We can adjust the function which finds the second smallest number in a list to find the second largest number in a list.

To find the second largest number in a list, we just need to keep track of the largest and second largest numbers, and loop over the list of numbers.

Below is an example of a Python function which gets the second largest number in a list of numbers.

list_of_numbers = [2, 1, 9, 8, 6, 3, 1, 0, 4, 5]
  
def findSecondLargest(lst):
    firstLargest = max(lst[0],lst[1])
    secondLargest = min(lst[0],lst[1])
    for i in range(2,len(lst)):
        if lst[i] > firstLargest:
            secondLargest = firstLargest
            firstLargest =lst[i]
        elif lst[i] > secondLargest and firstLargest > lst[i]:
            secondLargest = lst[i]
    return secondLargest

print(findSecondLargest(list_of_numbers))

#Output:
8

Hopefully this article has been useful for you to learn how to get the second smallest value in a list using Python.

Other Articles You'll Also Like:

  • 1.  Using Python to Read Random Line from File
  • 2.  pandas Duplicated – Find Duplicate Rows in DataFrame or Series
  • 3.  Calculate Distance Between Two Points in Python
  • 4.  Changing Python Turtle Size with turtlesize() Function
  • 5.  Using Python to Read File Character by Character
  • 6.  Using Python Selenium Webdriver to Refresh Page
  • 7.  Check if Line is Empty in Python
  • 8.  Check if Word is Palindrome Using Recursion with Python
  • 9.  Find Index of Minimum in List Using Python
  • 10.  pandas T Function – Transposing DataFrames with pandas

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

About The Programming Expert

the programming expert main image

Welcome to The Programming Expert. We are a group of US-based programming professionals who have helped companies build, maintain, and improve everything from simple websites to large-scale projects.

We built The Programming Expert to help you solve your programming problems with useful coding methods and functions in various programming languages.

Search

Learn Coding from Experts on Udemy

Looking to boost your skills and learn how to become a programming expert?

Check out the links below to view Udemy courses for learning to program in the following languages:

Copyright © 2023 · The Programming Expert · About · Privacy Policy