Python program to find second largest number in a list Last Updated : 01 Dec, 2024 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 infoAdvertise with us Next Article Python program to find second largest number in a list S Shivam_k Follow Improve Article Tags : Python Python Programs python-list Python list-programs Practice Tags : pythonpython-list Similar Reads Python Program to Find Largest Number in a List Finding the largest number in a list is a common task in Python. There are multiple way to do but the simplest way to find the largest in a list is by using Python's built-in max() function:Using max()Python provides a built-in max() function that returns the largest item in a list or any iterable. 3 min read Python program to find smallest number in a list In this article, we will discuss various methods to find smallest number in a list. The simplest way to find the smallest number in a list is by using Python's built-in min() function.Using min()The min() function takes an iterable (like a list, typle etc.) and returns the smallest value.Pythona = [ 2 min read Python program to find N largest elements from a list Given a list of integers, the task is to find N largest elements assuming size of list is greater than or equal o N. Examples : Input : [4, 5, 1, 2, 9] N = 2 Output : [9, 5] Input : [81, 52, 45, 10, 3, 2, 96] N = 3 Output : [81, 96, 52] A simple solution traverse the given list N times. In every tra 5 min read Python Program to Print Largest Even and Largest Odd Number in a List Auxiliary Given a list. The task is to print the largest even and largest odd number in a list. Examples: Input: 1 3 5 8 6 10 Output: Largest even number is 10 Largest odd number is 5 Input: 123 234 236 694 809 Output: Largest odd number is 809 Largest even number is 694 The first approach uses two 7 min read Python program to find second maximum value in Dictionary Dictionaries in Python is same as Associative arrays or Maps in other languages. Unlike lists, dictionaries stores data in key-value pair.Let's see how to find the second maximum value in a Python dictionary.Method #1: Naive Approach: A general approach is to find the largest value of the dictionary 3 min read Like