filmov
tv
How to Find the Second Largest Number in a List | EP-20 Find the Second Largest Number in a List
Показать описание
Method 1 : Using sort()- ascending
"""Algorithm
1. initialized list with values.
2.Print the initialized list
3.The sort() function is used to sort the list in descending order,or ascending order.
4.The sorted list is printed using the print() function.
5.The second largest number is printed using the print() function and the index of
the second largest number in the sorted list (which is 1, since list indexes start from(descending) 0 or
which is -2, since the list indesxes start from -1 ()ascending).
"""
list1 = [0,1,8,9,10,9,11,5]
print(list1)
print(list1)
print(list1[-2])
method 2: descending
print(list1)
print(list1[1],"descending")
method 3:using sorted method
print("finding the second lagerest element using sorted method: ",sorted(list1)[-2])
method 4: by removing max element from list
new_list =set(list1)
print(new_list)
print(new_list)
print(max(new_list))
#python #coding #tutorial #programming #algorithms #datascience #problem solving #secondlargestnumber #pythonprogramming #codingtutorial #pythontips #codingtricks #pythonchallenge #codingchallenge #learntocode #code #developer #programmer #softwaredeveloper #codinglife #codingcommunity #pythoncommunity #tech #technology #youtubetutorial #howto #tutorialvideo #PythonTutorial #LearnPython #DataStructures #TechTutorial #CodeWithMe #pythoninterviewquestions
Connect With Us:
—————————————
"""Algorithm
1. initialized list with values.
2.Print the initialized list
3.The sort() function is used to sort the list in descending order,or ascending order.
4.The sorted list is printed using the print() function.
5.The second largest number is printed using the print() function and the index of
the second largest number in the sorted list (which is 1, since list indexes start from(descending) 0 or
which is -2, since the list indesxes start from -1 ()ascending).
"""
list1 = [0,1,8,9,10,9,11,5]
print(list1)
print(list1)
print(list1[-2])
method 2: descending
print(list1)
print(list1[1],"descending")
method 3:using sorted method
print("finding the second lagerest element using sorted method: ",sorted(list1)[-2])
method 4: by removing max element from list
new_list =set(list1)
print(new_list)
print(new_list)
print(max(new_list))
#python #coding #tutorial #programming #algorithms #datascience #problem solving #secondlargestnumber #pythonprogramming #codingtutorial #pythontips #codingtricks #pythonchallenge #codingchallenge #learntocode #code #developer #programmer #softwaredeveloper #codinglife #codingcommunity #pythoncommunity #tech #technology #youtubetutorial #howto #tutorialvideo #PythonTutorial #LearnPython #DataStructures #TechTutorial #CodeWithMe #pythoninterviewquestions
Connect With Us:
—————————————