Skip to content

Write a Python program to find the second largest number in a list | Code

  • by

Use the sort() method and negative indexes to write a Python program to find the second largest number in a list.

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])

Output:

Write a Python program to find the second largest number in a list

Another way Removing the maximum element

Use the set() function, max() & remove() function.

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))

Output: 70

Do comment if you have any doubts or suggestions on this Python list topic.

Note: IDE: PyCharm 2021.3 (Community Edition)

Windows 10

Python 3.10.1

All Python Examples are in Python 3, so Maybe its different from python 2 or upgraded versions.

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.