Python Program to Find Second Largest Number From List

Program to find second largest number or element in list; In this tutorial, i am going to show you how to find second largest number from list in python.

And i will write some python program to find second largest element or number from list.

Python Program to Find Second Largest Number From a List

  • Python program to find second largest number in list using Sort() method
  • Python program to find second largest number in list using function
  • Python program to find second largest number in list using Max() method

Python program to find second largest number in list using Sort() method

See the following steps to write a python program to find the second largest element or number in the list using sort() method:

  • Get input length of list from user in program.
  • Define empty list in program.
  • Then, iterate the for loop and merge the numbers in the list.
  • Use the sort method to find second largest numbers from the list.
  • Print the results.
# Python program to find second largest number in a list
# using sort method
 
# make empty list
list1 = []
 
# take input number of elements in list
num = int(input("Enter number of elements in list: "))
 
# iterating till num to append elements in list
for i in range(1, num + 1):
    ele = int(input("Enter elements: "))
    list1.append(ele)
 
'''
# sort the list   
list1.sort()
     
# print second maximum element
print("Second largest element is:", list1[-2])
 
'''
 
# print second maximum element using sorted() method
print("Second largest element is:", sorted(list1)[-2])

After executing the program, the output will be:

Enter number of elements in list: 5
Enter elements: 10
Enter elements: 20
Enter elements: 4
Enter elements: 45
Enter elements: 90
Second largest element is: 45

Python program to find second largest number in list using function

See the below given steps to write a python program to find the second largest element or number in the list using custom function and max() method:

  • Take input the length of the list from user in program.
  • Next, iterate the for loop and add the number in the list.
  • Define function and implement logic to find second largest number from list.
  • Call above define function with list.
  • Print second largest number from list
# Python program to find second largest number in a list
# using custom function with max method
 
# make empty list
list1 = []
 
# take input number of elements in list
num = int(input("Enter number of elements in list: "))
 
# iterating till num to append elements in list
for i in range(1, num + 1):
    ele = int(input("Enter elements: "))
    list1.append(ele)
 
mx=max(list1[0],list1[1])
secondmax=min(list1[0],list1[1])
n =len(list1)
for i in range(2,n):
    if list1[i]>mx:
        secondmax=mx
        mx=list1[i]
    elif list1[i]>secondmax and \
        mx != list1[i]:
        secondmax=list1[i]
 
print("Second highest number is : ",\
      str(secondmax))

After executing the program, the output will be:

Enter number of elements in list: 5
Enter elements: 10
Enter elements: 20
Enter elements: 4
Enter elements: 45
Enter elements: 90
Second highest number is :  45

Python program to find second largest number in list using Max() method

See the below given steps to write a python program to find the second largest element or number in the list using max() and set() method:

  • Take input the length of the list from user in program.
  • Next, iterate the for loop and add the number in the list.
  • Create new list with set method
  • To remove first largest element from list using remove() method
  • Print second largest number from list
# Python program to find second largest number in a list
# using set and max method
 
# make empty list
list1 = []
 
# take input number of elements in list
num = int(input("Enter number of elements in list: "))
 
# iterating till num to append elements in list
for i in range(1, num + 1):
    ele = int(input("Enter elements: "))
    list1.append(ele)
    
# create new list using set
new_list = set(list1)
 
# delete the largest element from new list
new_list.remove(max(new_list))
print("Second largest element is:", max(new_list))

After executing the program, the output will be:

Enter number of elements in list: 5
Enter elements: 10
Enter elements: 20
Enter elements: 4
Enter elements: 45
Enter elements: 90
Second highest number is :  45

Recommended Python Tutorials

Be First to Comment

Leave a Reply

Your email address will not be published.