Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

second highest value in list python

# Python program to find second largest
# number in a list
 
# list of numbers
list1 = [10, 20, 4, 45, 99]
 
# new_list is a set of list1
new_list = set(list1)
 
# removing the largest element from temp list
new_list.remove(max(new_list))
 
# elements in original list are not changed
# print(list1)
 
print(max(new_list))
Comment

second highest value in list python

# 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))
Comment

PREVIOUS NEXT
Code Example
Python :: activate virtual environment python in linux 
Python :: how to activate venv python 
Python :: how to check if variable in python is of what kind 
Python :: how to read an xml file 
Python :: custom permission class django rest framework 
Python :: master python 
Python :: type() in python 
Python :: self object 
Python :: add column to dataframe pandas 
Python :: python while loop 
Python :: how to remove item from list in python 
Python :: python generators with for 
Python :: tuple python 
Python :: how to change datatype of column in pandas 
Python :: site:*.instagram.com 
Python :: min and max in python 
Python :: django serializer method field read write 
Python :: unzipping the value using zip() python 
Python :: Search for a symmetrical inner elements of a list python 
Python :: how to change the starting number for the loop count in pythin 
Python :: python calander from Programmer of empires but updated 
Python :: Spatial Reference arcpy 
Python :: python django creating products 
Python :: Define the learnable resizer utilities 
Python :: #Function in python without input method with multiple results: 
Python :: python parameter pack 
Python :: pool.map multiple arguments 
Python :: rtdpy ncstr 
Python :: how to use displacy 
Python :: center fig legend 
ADD CONTENT
Topic
Content
Source link
Name
9+3 =