Second Largest in List

Tags: List, Sorting

Given a list of integers, write a Python program to find the second-largest number in the list.

Example 1:

Input: [1,2,3,4,5] 
Output: 4

Example 2:

Input: [10,20,30,40,50] 
Output: 40

Consider using Python’s built-in sorting function.

def second_largest(nums):
    nums.sort()
    return nums[-2]

© Let’s Data Science

LOGIN

Unlock AI & Data Science treasures. Log in!