---
title: "Find the second largest number in a list in python with explanation."  
description: "Find the second largest number in a list in python with explanation."  
author: "Anubhav Sharma"  
published: 2025-04-23  
updated: 2025-04-27  
canonical: https://www.mindstick.com/forum/161531/find-the-second-largest-number-in-a-list-in-python-with-explanation  
category: "python"  
tags: ["python-3.4", "python"]  
reading_time: 2 minutes  

---

# Find the second largest number in a list in python with explanation.

Without using built-in [functions](https://www.mindstick.com/forum/160140/explain-the-role-of-functions-as-a-service-faas-in-serverless-computing) like `sort()` or `max()` twice.

## Replies

### Reply by Khushi Singh

Several approaches exist to locate the second largest number contained in a list within [Python language](https://www.mindstick.com/articles/338013/top-10-python-libraries-for-data-science-and-ai). The following step-by-step method built into Python can help you find the second largest number. Following is a step-by-step breakdown of the code along with its explanation.

## Explanation:

The descending order sorting of the list allows us to obtain the second element directly. Causing inefficiency in terms of time complexity is sorting the list since it operates at O(n log n) for an extended list.

A better and faster approach involves one single iteration of the list and maintaining a record of the largest pair of numbers during each pass. The O(n) time complexity of this method proves faster than other methods when working with extensive lists.

## Code Example

```python
def find_second_largest(numbers):
   if len(numbers) < 2:
       return None  # Not enough elements to find the second largest

   largest = second_largest = float('-inf')

   for num in numbers:
       if num > largest:
           second_largest = largest
           largest = num
       elif num > second_largest and num != largest:
           second_largest = num

   return second_largest
# Example usage
numbers = [12, 35, 1, 10, 34, 1]
result = find_second_largest(numbers)
print("Second largest number is:", result)
```

## Explanation of the Code:

## Initialization:

- The program begins by assigning -inf through both second_largest and largest variables. The preliminary values have a negative infinite value so every number in the list exceeds them by default.

## Looping through the list:

- The code moves through each number present in the list.
- When a number exceeds largest the code indicates we have discovered an updated largest number value. The original largest number then transitions into the position of the second largest.
- Updating the value of second_largest occurs when the number exceeds second_largest but does not exceed largest.

## Edge case:

- A return value of None takes place when the list contains less than two elements because the process of finding the second largest number becomes impossible.

## Output

For the input list `[12, 35, 1, 10, 34, 1]`, the output will be:

```plaintext
Second largest number is: 34
```

This method ensures that you only loop through the list once, making it an efficient solution.


---

Original Source: https://www.mindstick.com/forum/161531/find-the-second-largest-number-in-a-list-in-python-with-explanation

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
