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

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

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 :: Yahoo! Finance pyhton 
Python :: merge two columns name in one header pandas 
Python :: python threading return value 
Python :: python sort algorithm 
Python :: how to change value of categorical variable in python 
Python :: get ticks pygame 
Python :: remove all elements from list python by value 
Python :: poppler on colab 
Python :: pandas count number of repetitions of each diferent value 
Python :: check runtime python 
Python :: python split 
Python :: python remove one character from a string 
Python :: series astype 
Python :: atan2 of number python 
Python :: jupyter matplotlib 
Python :: convert list of lists to numpy array matrix python 
Python :: python dictionary pop key 
Python :: python dictionary comprehensions 
Python :: remove key from dictionary 
Python :: python np get indices where value 
Python :: matplotlib axis labels 
Python :: python anytree 
Python :: percent in pandas 
Python :: get index of all element in list python 
Python :: activate virtual environment 
Python :: pyinstaller pymssql 
Python :: how to sort a list in python 
Python :: df index drop duplicates 
Python :: how to merge two column pandas 
Python :: transform image to rgb python 
ADD CONTENT
Topic
Content
Source link
Name
8+2 =