• 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
  • VBA
  • 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.  Get First Key and Value in Dictionary with Python
  • 2.  Using Python to Read File Character by Character
  • 3.  Convert True to 1 in Python
  • 4.  How to Return Nothing in Python from Function
  • 5.  Python Coin Flip – How to Simulate Flipping a Coin in Python
  • 6.  Python Even or Odd – Check if Number is Even or Odd Using % Operator
  • 7.  Get Last Day of Month Using Python
  • 8.  Format Numbers as Currency with Python
  • 9.  Multiple Condition if Statements in Python
  • 10.  Python isfinite() Function – Check if Number is Finite with math.isfinite()

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

The Programming Expert is a compilation of hundreds of code snippets to help you find solutions to your problems in Python, JavaScript, PHP, HTML, SAS, and more.

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 © 2022 · The Programming Expert · About · Privacy Policy