# Python Program to find Second Largest Number in a List

Write a Python Program to find the Second Largest Number in a List with a practical example.

## Python Program to find Second Largest Number in a List Example 1

This python program allows user to enter the length of a List. Next, we used For Loop to add numbers to the list in Python.

The sort function in python sorts the List elements in ascending order. Next, we are using the Python Index position to print Last but one element in a List.

```# Python Program to find Second Largest Number in a List

NumList = []
Number = int(input("Please enter the Total Number of List Elements: "))
for i in range(1, Number + 1):
value = int(input("Please enter the Value of %d Element : " %i))
NumList.append(value)

NumList.sort()

print("The Largest Element in this List is : ", NumList[Number - 2])```

## Python Program to find Second Largest Number in a List Example 2

This program sort the list items in Ascending order. Next, we used reverse function to reverse the list items. Lastly, we used index position 1 to print the second element in a list.

```# Python Program to find Second Largest Number in a List

NumList = []
Number = int(input("Please enter the Total Number of List Elements: "))
for i in range(1, Number + 1):
value = int(input("Please enter the Value of %d Element : " %i))
NumList.append(value)

NumList.sort()
NumList.reverse()
print("The Largest Element in this List is : ", NumList)```

The Second Largest Number in a Python List output

``````Please enter the Total Number of List Elements: 5
Please enter the Value of 1 Element : 20
Please enter the Value of 2 Element : 56
Please enter the Value of 3 Element : 78
Please enter the Value of 4 Element : 97
Please enter the Value of 5 Element : 60
The Largest Element in this List is :  78``````

## Python Program to find Second Largest Number in a List Example 3

In this program, we are not using any built-in function, such as sort or reverse function. For this, we use For Loop

```# Python Program to find Second Largest Number in a List

NumList = []
Number = int(input("Please enter the Total Number of List Elements: "))

for i in range(1, Number + 1):
value = int(input("Please enter the Value of %d Element : " %i))
NumList.append(value)

first = second = NumList
for j in range(1, Number):
if(NumList[j] > first):
second = first
first = NumList[j]
elif(NumList[j] > second and NumList[j] < first):
second = NumList[j]

print("The Largest Element in this List is : ", first)
print("The Second Largest Element in this List is : ", second)```

The Second Largest Number in a Python List output

``````Please enter the Total Number of List Elements: 4
Please enter the Value of 1 Element : 55
Please enter the Value of 2 Element : 57
Please enter the Value of 3 Element : 22
Please enter the Value of 4 Element : 3
The Largest Element in this List is :  57
The Second Largest Element in this List is :  55``````

From the above Python Program to return Second Largest Number in a List screenshot, you can observe that the User inserted values are

NumList = {55, 57, 22, 3}
first = second = NumList = 55

First Iteration – for 1 in range(1, 4) – Condition is true
So, it starts executing If statement inside the loop until the condition fails.
if(NumList[j] > first) inside the for loop is True because (57 > 55)
second = first = 55
first = NumList = 57

Second Iteration: for 2 in range(1, 4) – Condition is true
If (NumList > first)  =  (22 > 57) – Condition is False. So, it enters into elif statement
elif(NumList > second and NumList < first)
elif(22 > 55 and 22 < 57) –  Condition is False

Third Iteration: for 3 in range(1, 4) – Condition is true
If (3 > 57) – Condition is False
elif(3 > 55 and 3 < 57) –  Condition is False

Fourth Iteration: for 4 in range(1, 4) – Condition is False. So, it exits from the loop.