CameraIcon
CameraIcon
SearchIcon
MyQuestionIcon
MyQuestionIcon
1
You visited us 1 times! Enjoying our articles? Unlock Full Access!
Question

Write a function to return the second largest number from a list of numbers.

Open in App
Solution

Program:
def secLargestNum(list1):
#Sorting the list in ascending order
list1.sort()

#Returning the second last index of list1
#List with negative indexing will start from the end in reverse order with -1 as the index of the last element
secondLast = list1[-2]


#or len(list1)-2 can be used which will return second last element
#secondLast = list1[len(list1)-2]
return secondLast

list1 = [1, 2, 3, 4, 5, 6, 7, 8, 9]

#Using secLargestNum function to get the function
sec_num = secLargestNum(list1)

#Printing all the elements for the list
print("The elements of the list",list1)

#Printing the second largest num
print("\nThe second-largest number of the list:",sec_num)


OUTPUT:
The elements of the list [1, 2, 3, 4, 5, 6, 7, 8, 9]

The second-largest number of the list: 8

flag
Suggest Corrections
thumbs-up
1
Join BYJU'S Learning Program
similar_icon
Related Videos
thumbnail
lock
QUANTITATIVE APTITUDE
Watch in App
Join BYJU'S Learning Program
CrossIcon