Python program to find the second largest number in a list
Simple example code printing the second last element. Simply just sort the given list in ascending order and get the second last element in the list.
list1 = [10, 20, 70, 40, 90] # sorting the list list1.sort() print("Second largest element is:", list1[-2])
Another way Removing the maximum element
list1 = [10, 20, 70, 40, 90] new_list = set(list1) # removing the largest element from list1 new_list.remove(max(new_list)) print(max(new_list))
Do comment if you have any doubts or suggestions on this Python list topic.
Note: IDE: PyCharm 2021.3 (Community Edition)
All Python Examples are in Python 3, so Maybe its different from python 2 or upgraded versions.
Degree in Computer Science and Engineer: App Developer and has multiple Programming languages experience. Enthusiasm for technology & like learning technical.