Click here to Skip to main content
15,921,169 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Finding the second largest
element from a given list
method 1 Sort the list and
remove the largest element and
print max
method 2 sort the list and
using negative indexing print
the second max
method 3 without sorting find
the max of list then find the
next max of the list
by comparing the max
Design algorithm for the
above problem

What I have tried:

def find Largest(list):
sec_Larg = 0
largest = min(list)
for i in range(len(list)):
if list[i] > largest:
sec_Larg = 
largest = list[i]
sec_Larg = 
max (sec_Larg, list[i])
return sec_Larg
def sec_larg_elem_m1(list):
 max_elem = max (list [:-1])
 return max_elem
def sec_larg_elem_m2(list):
 sec_larg = list [-2]
 return sec_larg
def sec_larg_elem_m3(list):
 max_element = max(list)
 sec_larg = max(list)
 return sec_larg
list = [1,5,2,8,9,3,7]
print ("Second largest number:
print ("Second largest number 
using Method-1: 
print ("Second largest number 
using Method-2: 
print ("Second largest number 
using Method-3:
Updated 22-Mar-23 6:53am
Richard MacCutchan 22-Mar-23 10:59am    
Do you have an actual question?

1 solution

Start here: Python Indentation[^]

Unless you indent Python code correctly - and I mean exactly correctly - it will not work as you intended.
Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900