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
- Python Print List Elements in Different Way
- How to Input List From User in Python
- Python Add and Remove Elements From List
- Python: Add/Insert Element at Specified Index in List
- Python Program to Remove ith/Nth Occurrence of Given Word in List
- Python Program to Sort List in Ascending and Descending Order
- Python to Find the Differences of Two Lists
- Python to Find Minimum and Maximum Elements of List
- Python Programs to Split Even and Odd Numbers in Separate List
- Python Program to Create Two Lists with First Half and Second Half Elements of Given List
- Python Program to Swap Two Elements in a List
- Python Program to Reverse List
- How To Select Random Item From A List In Python