• 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 Largest Value in List

Using Python to Find Second Largest Value in List

February 24, 2022 Leave a Comment

In Python, we can find the second largest 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 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

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

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

print(findSecondLargest(list_of_numbers))

#Output:
8

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

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

print(findSecondLargest(list_of_numbers))

#Output:
8

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 maximum of a list of numbers.

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

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

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

Below is an example of a Python function which gets the second largest value 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

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

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

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

We can access the second to last element of a list by accessing the ‘-2’ position.

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

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

print(findSecondLargest(list_of_numbers))

#Output:
8

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

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

First, we need to use the max() function to get the maximum value of the list. Then we use the list remove() function to remove the maximum value. We can then use the max() function again to get the second biggest number.

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

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

print(findSecondLargest(list_of_numbers))

#Output:
8

How to Find Second Smallest Value in a List Using Python

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

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

Below is an example of a Python function which gets the second smallest number 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

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

Other Articles You'll Also Like:

  • 1.  Check if Character is Letter in Python
  • 2.  How to Cube Numbers in Python
  • 3.  pandas floor – Find the Floor of a Column Using Numpy floor
  • 4.  math.degrees() Python – How to Convert Radians to Degrees in Python
  • 5.  Convert Set to List in Python
  • 6.  Using Python Selenium Webdriver to Refresh Page
  • 7.  Ceiling Division in Python
  • 8.  Remove Element from Set in Python
  • 9.  pandas dropna – Drop Rows or Columns with NaN in DataFrame
  • 10.  How to Read Pickle File from AWS S3 Bucket Using Python

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