Finding the Second Largest Number in Python
Finding specific values from a Python list is a common programming task. While finding the largest or smallest element is straightforward, finding the second-largest number can be approached in several different ways.
Python provides multiple techniques for solving this problem. In this tutorial, we will explore four approaches: sorting the list, removing the maximum value, accepting input using a loop, and traversing the list without sorting.
Table of Contents

Method 1: Sorting the List
The simplest approach is to sort the list in ascending order. Once the elements are arranged, the second-largest value can be obtained using negative indexing.
Example
# Program to find the second-largest number
# Creating the list
list_val = [20, 30, 40, 25, 10]
# Sorting the list
list_val.sort()
# Getting the second-largest element
print("The second largest element of the list is:", list_val[-2])
Output
The second largest element of the list is: 30
Explanation
- The
sort()method arranges the elements of the list in ascending order. - After sorting,
list_val[-2]accesses the second-to-last element, which is the second-largest value for this list.
Method 2: Removing the Maximum Element
Another technique is to first remove the largest value and then determine the maximum value among the remaining elements. A set is used in this approach to remove duplicate values.
Example
# Program to find the second-largest number
# Creating the list
list_val = [20, 30, 40, 25, 10]
# Removing duplicate values
unique_vals = set(list_val)
# Removing the largest element
unique_vals.remove(max(unique_vals))
# Finding the second-largest element
print("The second largest element of the list is:", max(unique_vals))
Output
The second largest element of the list is: 30
Explanation
- The list is converted into a set so duplicate values are removed.
- The largest value is obtained using
max()and removed with theremove()method. - The maximum value remaining in the set is then the second-largest value from the original list.
Method 3: Using a For Loop
In this method, the list values are entered by the user. The program stores the entered values in a list, sorts them, and then retrieves the second-largest element.
Example
# Program to find the second-largest number
# Creating an empty list
list_val = []
# Taking the number of elements from the user
num_list = int(input("Enter the number of elements in the list: "))
for i in range(num_list):
element = int(input(f"Enter element {i + 1}: "))
list_val.append(element)
# Sorting the list
list_val.sort()
# Displaying the second-largest element
print("The second largest element is:", list_val[-2])
Output
Enter the number of elements in the list: 5
Enter element 1: 10
Enter element 2: 20
Enter element 3: 30
Enter element 4: 40
Enter element 5: 50
The second largest element is: 40
Explanation
- The program first asks the user to enter the number of elements.
- Each element is entered and added to
list_val. - The list is sorted in ascending order.
- The second-to-last element is accessed using
list_val[-2].
Method 4: Traversing the List
The second-largest number can also be calculated without sorting the list. In this approach, the program traverses each element and continuously updates the largest and second-largest values.
Example
def calc_second_largest(arr):
largest = second_largest = float('-inf')
for num in arr:
if num > largest:
second_largest = largest
largest = num
elif num > second_largest and num != largest:
second_largest = num
return second_largest
# Example list
list_val = [20, 30, 40, 25, 10]
print(
"The second largest element of the list is:",
calc_second_largest(list_val)
)
Output
The second largest element of the list is: 30
Explanation
- Initialize the variables:
largestandsecond_largestare initialized with negative infinity. - Traverse the list: Each number is checked against the current largest and second-largest values.
- Update the values: When a number greater than
largestis found, the previous largest value becomes the second-largest value. - Return the result: After all elements have been processed, the function returns
second_largest.
Comparison of the Four Methods
| Method | Approach | Main Idea |
|---|---|---|
| Sorting the List | Sort the complete list | Access the second-to-last element |
| Removing the Maximum | Use a set and remove the largest value | Find the maximum from the remaining values |
| Using a For Loop | Accept values dynamically | Sort the entered values and access the second-largest |
| Traversing the List | Scan the list directly | Track largest and second-largest values |
Summary
Complete Advance AI Topics: Click Here
SQL Tutorial: Click Here
YT:- DecodeIT
In this tutorial, we explored four different ways to find the second-largest number in Python.
- Sorting: A straightforward method that sorts the list and uses negative indexing.
- Removing the Maximum: Uses a set to remove duplicate values before removing the largest value.
- For Loop: Allows the user to enter list elements dynamically before finding the result.
- List Traversal: Finds the second-largest value by scanning the list without sorting it.
Each method provides a different way to approach the same programming problem. Understanding these techniques can help you choose an appropriate solution when working with Python lists and numerical data.
Frequently Asked Questions
1. How do you find the second-largest number in Python?
The second-largest number can be found using several approaches, including sorting the list, removing the largest value, or traversing the list while tracking the largest and second-largest values.
2. How can you find the second-largest number without sorting?
You can traverse the list and maintain two variables, one for the largest number and another for the second-largest number.
3. What does list_val[-2] mean?
list_val[-2] accesses the second-to-last element of a list. When the list is sorted in ascending order, this represents the second-largest value.
4. Why is a set used in the second method?
A set is used to remove duplicate values before identifying the second-largest distinct number.
5. What does float(‘-inf’) mean?
float('-inf') represents negative infinity and is used to initialize the largest and second-largest tracking variables with a value smaller than normal numeric values.
6. Can the second-largest number be found using a function?
Yes. The traversal approach defines a function named calc_second_largest() that accepts a list and returns its second-largest distinct value.
Conclusion
Finding the second-largest number is a useful Python programming problem for understanding lists, sorting, loops, sets, negative indexing, and conditional logic. Python allows the same problem to be solved using different techniques depending on the requirements.
For simple cases, sorting provides an easy solution. When you want to avoid sorting, traversing the list and tracking the largest values provides another approach. The set-based method is useful when duplicate values need to be removed, while the input-based approach demonstrates how user-provided values can be processed.
Keywords: Finding the Second Largest Number in Python, Finding the Second Largest Number in Python,second largest number in Python without sort, finding the second largest number in Python, second largest number in Python using if else, find the second largest number in list Python using function, second largest number in Python HackerRank solution, how to find second largest number among 3 numbers in Python, find index of second largest number in array Python, find second largest number in array without sorting, Python program to find first and second largest element in a list,Finding the Second Largest Number in Python