Open In App

Largest, Smallest, Second Largest, Second Smallest in a List-Python

Last Updated : 03 Mar, 2025
Summarize
Comments
Improve
Suggest changes
Share
Like Article
Like
Report

We are given a list of numbers and our task is to find the largest, smallest, second largest and second smallest elements in the list. For example, if the list is [4, 1, 7, 3, 9], the largest is 9, the smallest is 1, the second largest is 7 and the second smallest is 3. Let's explore different ways to solve this problem efficiently in Python.

Using min() and max()

This approach utilizes Python’s built-in max() and min() functions to find the largest and smallest elements. By removing them from the list, we can efficiently determine the second largest and second smallest values. This method is effective as it avoids unnecessary sorting.

Python
a = [12, 45, 2, 41, 31, 10, 8, 6, 4]

# find largest and smallest values
l1 = max(a)
s1 = min(a)

# remove them from the list
a.remove(l1)
a.remove(s1)

# find second largest and second smallest
l2 = max(a)
s2 = min(a)

print(f"Largest: {l1}, Smallest: {s1}, Second Largest: {l2}, Second Smallest: {s2}")

Output
Largest: 45, Smallest: 2, Second Largest: 41, Second Smallest: 4

Explanation: This code first finds the largest and smallest elements using max() and min(), then removes them to avoid interference. It then applies max() and min() again to determine the second largest and second smallest elements from the remaining numbers.

Using heap

heapq module provides a structured way to find the smallest and largest elements efficiently. Using nsmallest(2, a) and nlargest(2, a), we can extract the required values in a well-optimized manner. This method is particularly useful when working with large datasets where structured extraction is preferred over manual iteration.


Python
import heapq

a = [12, 45, 2, 41, 31, 10, 8, 6, 4]

# get two smallest and two largest elements
s1, s2 = heapq.nsmallest(2, a)
l1, l2 = heapq.nlargest(2, a)

print(f"Largest: {l1}, Smallest: {s1}, Second Largest: {l2}, Second Smallest: {s2}")

Output
Largest: 45, Smallest: 2, Second Largest: 41, Second Smallest: 4

Explanation: heapq.nsmallest(2, a) retrieves the two smallest values, while heapq.nlargest(2, a) extracts the two largest values.

Using for loop

A single pass through the list can determine all four values efficiently by maintaining only four variables. This method eliminates the need for extra function calls or sorting, making it one of the fastest and most memory-efficient solutions. It ensures minimal computation while keeping the logic straightforward.

Python
a = [12, 45, 2, 41, 31, 10, 8, 6, 4]

l1 = l2 = float('-inf')  # largest and second largest
s1 = s2 = float('inf')    # smallest and second smallest

for n in a:
    # update largest and second largest
    if n > l1:
        l2, l1 = l1, n
    elif n > l2:
        l2 = n

    # update smallest and second smallest
    if n < s1:
        s2, s1 = s1, n
    elif n < s2:
        s2 = n

print(f"Largest: {l1}, Smallest: {s1}, Second Largest: {l2}, Second Smallest: {s2}")

Output
Largest: 45, Smallest: 2, Second Largest: 41, Second Smallest: 4

Explanation: loop updates l1, l2 for larger values and s1, s2 for smaller ones, ensuring l1 and s1 store the largest and smallest, while l2 and s2 hold the second largest and second smallest.

Using sorting

Sorting the list and directly selecting the required elements is the most intuitive approach but not the most efficient. While simple to implement, sorting the entire list takes unnecessary extra steps when only four values are needed. This method is best suited for small datasets where performance is not a concern.

Python
a = [12, 45, 2, 41, 31, 10, 8, 6, 4]
a.sort()

print(f"Largest: {a[-1]}, Smallest: {a[0]}, Second Largest: {a[-2]}, Second Smallest: {a[1]}")

Output
Largest: 45, Smallest: 2, Second Largest: 41, Second Smallest: 4

Explanation: This code sorts a in ascending order, allowing direct access to the largest (a[-1]), second largest (a[-2]), smallest (a[0]) and second smallest (a[1]) elements.


Next Article

Similar Reads