Here, we are going to write python program to find third largest/ smallest number in a list
python source code
# Python Program to find Largest and Smallest Number in a List
# Creating list
nlist = []
# Reading numbers from user
n = int(input("Enter the Total Number of List Elements: "))
# Using for loop to add numbers in list
for i in range(1, n + 1):
value = int(input("Enter the Value of %d Element : " %i))
nlist.append(value)
# Sort list elements
sorted_list = sorted(nlist)
print("Sorted elements in list : ",sorted_list)
# Displaying smallest and largest element in list
print("The Third Smallest Element in this List is : ", sorted_list[2])
print("The Third Largest Element in this List is : ", sorted_list[-3])
Output:
Enter the Total Number of List Elements: 10
Enter the Value of 1 Element : 33
Enter the Value of 2 Element : 44
Enter the Value of 3 Element : 11
Enter the Value of 4 Element : 222
Enter the Value of 5 Element : 55
Enter the Value of 6 Element : 67
Enter the Value of 7 Element : 88
Enter the Value of 8 Element : 99
Enter the Value of 9 Element : 35
Enter the Value of 10 Element : 65
Sorted elements in list : [11, 33, 35, 44, 55, 65, 67, 88, 99, 222]
The Third Smallest Element in this List is : 35
The Third Largest Element in this List is : 88
<