Python 3 Script to Find Second Largest Element in an Array or List Using For Loop Full Tutorial For Beginners

Python 3 Script to Find Second Largest Element in an Array or List Using For Loop Full Tutorial For Beginners

 

Welcome folks today in this blog post we will be finding second largest element in an array or list in python. All the full source code of the application is given below.

 

 

Get Started

 

 

In order to get started you need to make an app.py file and copy paste the following code which is shown below

 

app.py

 

# Python program to find second largest
# number in a list

# list of numbers - length of 
# list should be at least 2
list1 = [10, 20, 4, 45, 99]

mx=max(list1[0],list1[1]) 
secondmax=min(list1[0],list1[1]) 
n =len(list1)
for i in range(2,n): 
    if list1[i]>mx: 
        secondmax=mx
        mx=list1[i] 
    elif list1[i]>secondmax and \
        mx != list1[i]: 
        secondmax=list1[i]

print("Second highest number is : ",\
    str(secondmax))

 

See also  Python 3 Tkinter Script to Build Picture Sliding Puzzle Strategy Game GUI Desktop App Full Project For Beginners

 

And now if you execute it by given command

 

python app.py

 

 

Leave a Reply