Use app×
Join Bloom Tuition
One on One Online Tuition
JEE MAIN 2025 Foundation Course
NEET 2025 Foundation Course
CLASS 12 FOUNDATION COURSE
CLASS 10 FOUNDATION COURSE
CLASS 9 FOUNDATION COURSE
CLASS 8 FOUNDATION COURSE
0 votes
172 views
in Computer by (51.9k points)
closed by

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

1 Answer

+1 vote
by (49.5k points)
selected by
 
Best answer
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

Related questions

0 votes
1 answer
0 votes
1 answer
0 votes
1 answer
asked Jun 15, 2023 in R by kvdevika (112k points)
0 votes
1 answer

Welcome to Sarthaks eConnect: A unique platform where students can interact with teachers/experts/students to get solutions to their queries. Students (upto class 10+2) preparing for All Government Exams, CBSE Board Exam, ICSE Board Exam, State Board Exam, JEE (Mains+Advance) and NEET can ask questions from any subject and get quick answers by subject teachers/ experts/mentors/students.

Categories

...