Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

Python program to find second largest number in a list

# 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]
    elif mx == secondmax and 
        secondmax != list1[i]:
          secondmax = list1[i]
 
print("Second highest number is : ",
      str(secondmax))
Comment

write a python program to find the second largest number in a list

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]
    elif mx == secondmax and 
        secondmax != list1[i]:
          secondmax = list1[i]
 
print("Second highest number is : ",
      str(secondmax))
Comment

PREVIOUS NEXT
Code Example
Python :: geopandas read postgis SQL 
Python :: python lock file 
Python :: Fastest way to Convert Integers to Strings in Pandas DataFrame 
Python :: convert all sizes to terabytes pandas 
Python :: how to check if text is lower in python 
Python :: tuple push 
Python :: fill column based on values of another column 
Python :: regex find all sentences python 
Python :: pip not recognized 
Python :: python trim zero off end of list 
Python :: pandas show all dataframe method 
Python :: plt.semilogx 
Python :: open file in python 
Python :: tensorflow create custom initializer 
Python :: what is modulus in python 
Python :: Merge two Querysets in Python Django while preserving Queryset methods 
Python :: key pressed pygame 
Python :: jupyter notebook bold text 
Python :: python datetime to unix timestamp 
Python :: python construct a string 
Python :: image data generator keras with tf.data.Data.from_generator 
Python :: Split a list based on a condition 
Python :: how to refer to all columns in pandas 
Python :: print("hello world") 
Python :: how to start coding in python 
Python :: df mask 
Python :: multiple line comments 
Python :: database with python connection 
Python :: python increment 
Python :: how to remove text from plot in python 
ADD CONTENT
Topic
Content
Source link
Name
5+6 =