Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

how to print second largest number in python

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))
      
 Output:-     
      
Second highest number is :  45
Comment

PREVIOUS NEXT
Code Example
Python :: Use operator in python list 
Python :: replace nan from another column 
Python :: python calculator 
Python :: e in python 
Python :: python turtle tutorial 
Python :: python calling method from constructor 
Python :: how to find the last element of list in python 
Python :: python repr() 
Python :: create payment request in stripe 
Python :: control flow in python 
Python :: how to use djoser signals 
Python :: variable python 
Python :: pyautogui 
Python :: How To Remove Elements From a Set using remove() function in python 
Python :: data encapsulation in python 
Python :: array sort in python 
Python :: pybase64 
Python :: python declare 2d list 
Python :: python convert time 
Python :: import python module 
Python :: type() in python 
Python :: string length python 
Python :: read yml file in python 
Python :: syntax of ternary operator 
Python :: site:*.instagram.com 
Python :: commands.has_role discord.py 
Python :: replace NaN value in pandas data frame with zeros 
Python :: python nasa api 
Python :: if statement collection python 
Python :: ex:deleate account 
ADD CONTENT
Topic
Content
Source link
Name
8+5 =