Python Program to Find Second Largest Number in List

Python Program to Find Second Largest Number in List

Python program to find second largest number in list; Through this tutorial, you will learn how to find second largest number from list in python.

Python Program to Find Second Largest Number in 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

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

  • Take input the length of the list from user in program.
  • Next, iterate the for loop and add the numbers in the list.
  • Find the second largest numbers from the list using sort method.
  • 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

Use the following 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

Use the following 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 List Programs

  1. Python Print List Elements in Different Way
  2. How to Input List From User in Python
  3. Python Add and Remove Elements From List
  4. Python: Add/Insert Element at Specified Index in List
  5. Python Program to Remove ith/Nth Occurrence of Given Word in List
  6. Python Program to Sort List in Ascending and Descending Order
  7. Python to Find the Differences of Two Lists
  8. Python to Find Minimum and Maximum Elements of List
  9. Python Programs to Split Even and Odd Numbers in Separate List
  10. Python Program to Create Two Lists with First Half and Second Half Elements of Given List
  11. Python Program to Swap Two Elements in a List
  12. Python Program to Reverse List
  13. How To Select Random Item From A List In Python

AuthorAdmin

My name is Devendra Dode. I am a full-stack developer, entrepreneur, and owner of Tutsmake.com. I like writing tutorials and tips that can help other developers. I share tutorials of PHP, Python, Javascript, JQuery, Laravel, Livewire, Codeigniter, Node JS, Express JS, Vue JS, Angular JS, React Js, MySQL, MongoDB, REST APIs, Windows, Xampp, Linux, Ubuntu, Amazon AWS, Composer, SEO, WordPress, SSL and Bootstrap from a starting stage. As well as demo example.

Leave a Reply

Your email address will not be published. Required fields are marked *