Python program to find second largest number in a list Last Updated : 11 Jul, 2025 Comments Improve Suggest changes Like Article Like Report In this article, we will explore various methods to find second largest number in a list. The simplest way to find is by using a loop. Using LoopUse a loop (for loop) to iterate over the list and keep two variables max1 and max2 to keep track of largest and second largest number of list. Python a = [10, 20, 4, 45, 99] # Initialize largest (max1) # and second largest (max2) to negative infinity max1 = max2 = float('-inf') # Loop through each number in list for n in a: # If the current number is greater # than largest found so far if n > max1: # Update second largest to the previous largest max2 = max1 # Update largest to the current number max1 = n # If current number is less than largest # but greater than second largest elif n > max2 and n != max1: # Update second largest to current number max2 = n print(max2) Output45 Let's explore other different method to find second largest number in a list:Table of ContentUsing SortingUsing heapq.nlargest()Using SortingOne of the simplest ways to find the second largest number is by sorting list in descending order. Once the list is sorted the second largest number will be at index 1. Python a = [10, 20, 4, 45, 99] # Sorting the list in descending order a.sort(reverse=True) # Second largest number will be at index 1 print(a[1]) Output45 Explanation:The list is sorted in descending order with a.sort(reverse=True).The largest number is at index 0 and the second largest is at index 1.Note: While this method is simple but sorting the list can be less efficient when dealing with large datasets.Using heapq.nlargest()The heapq.nlargest() function provides a simple and efficient way to find the largest elements in a list. We can use it to find the two largest numbers and access the second largest directly. Python import heapq a = [10, 20, 4, 45, 99] # Get the two largest numbers using heapq.nlargest top_two = heapq.nlargest(2, a) # The second largest number is at index 1 print(top_two[1]) Output45 Explanation:heapq.nlargest(2, a) returns the two largest numbers from the list.The second largest number is at index 1 of the returned list.Note: This approach is quick and efficient when we need to find the largest k elements. Comment More info S Shivam_k Follow Improve Article Tags : Python Python Programs python-list Python list-programs Explore Python FundamentalsPython Introduction 3 min read Input and Output in Python 4 min read Python Variables 5 min read Python Operators 5 min read Python Keywords 2 min read Python Data Types 7 min read Conditional Statements in Python 3 min read Loops in Python - For, While and Nested Loops 6 min read Python Functions 5 min read Recursion in Python 4 min read Python Lambda Functions 5 min read Python Data StructuresPython String 5 min read Python Lists 4 min read Python Tuples 4 min read Python Dictionary 3 min read Python Sets 6 min read Python Arrays 7 min read List Comprehension in Python 4 min read Advanced PythonPython OOP Concepts 11 min read Python Exception Handling 5 min read File Handling in Python 4 min read Python Database Tutorial 4 min read Python MongoDB Tutorial 2 min read Python MySQL 9 min read Python Packages 12 min read Python Modules 7 min read Python DSA Libraries 15 min read List of Python GUI Library and Packages 3 min read Data Science with PythonNumPy Tutorial - Python Library 3 min read Pandas Tutorial 6 min read Matplotlib Tutorial 5 min read Python Seaborn Tutorial 15+ min read StatsModel Library- Tutorial 4 min read Learning Model Building in Scikit-learn 8 min read TensorFlow Tutorial 2 min read PyTorch Tutorial 7 min read Web Development with PythonFlask Tutorial 8 min read Django Tutorial | Learn Django Framework 7 min read Django ORM - Inserting, Updating & Deleting Data 4 min read Templating With Jinja2 in Flask 6 min read Django Templates 7 min read Python | Build a REST API using Flask 3 min read How to Create a basic API using Django Rest Framework ? 4 min read Python PracticePython Quiz 3 min read Python Coding Practice 1 min read Python Interview Questions and Answers 15+ min read Like