;

Python Program to find the Second Largest Number from a List


Tutorialsrack 01/05/2020 Python

In this Python program, we will learn how to find the second largest element from a given List.

There are various ways to find the second largest number from a list. In this program, we used the python built-in functions such as set(), min(), max(), sort(), reverse() or without using any built-in function.

Here is the source code of the program to find the second largest element from a given List.

Program 1: Python Program to find the Second Largest Number from a List Using sort() Function

In this program, we will use the sort() function to sort the elements of the given list in ascending order and get the second largest element from the list by subtracting 2 from the list length.

Python Program to find the Second Largest Number from a List Using sort() Function
# Python Program to find the Second Largest Number from a List Using sort() Function

NumList = []
Number = int(input("Enter the Total Number of List Elements: "))
for i in range(1, Number + 1):
    value = int(input("Enter the Value of %d Element: " %i))
    NumList.append(value)
    
# Print the List Entered By the User
print("List: ", NumList)

NumList.sort()

print("The Second Largest Element in the Given List is: ", NumList[Number - 2])
Output

Enter the Total Number of List Elements: 6

Enter the Value of 1 Element: 12

Enter the Value of 2 Element: 42

Enter the Value of 3 Element: 4512

Enter the Value of 4 Element: 541

Enter the Value of 5 Element: 812

Enter the Value of 6 Element: 642

List:  [12, 42, 4512, 541, 812, 642]

The Second Largest Element in the Given List is:  812

Program 2: Python Program to find the Second Largest Number from a List Using sort() and reverse() function

In this program, we will sort() function to sort the elements of a list in ascending order and reverse() function to reverse the element of a given list from ascending to descending order and get the element of the second index position from the list.

hdfilsdhfosd fposdfjoisdhfiusdfh oisdfh osidfh

Python Program to find the Second Largest Number from a List Using sort() and reverse() function
# Python Program to find the Second Largest Number from a List Using sort() and reverse() function

NumList = []

# Take the Input from the User
Number = int(input("Enter the Total Number of List Elements: "))
for i in range(1, Number + 1):
    value = int(input("Enter the Value of %d Element: " %i))
    NumList.append(value)
    
# Print the List Entered By the User
print("\nList: ", NumList)    

NumList.sort()
NumList.reverse()
print("\nThe Second Largest Element in the Given List is: ", NumList[1])
Output

Enter the Total Number of List Elements: 6

Enter the Value of 1 Element: 123

Enter the Value of 2 Element: 524

Enter the Value of 3 Element: 654

Enter the Value of 4 Element: 458

Enter the Value of 5 Element: 253

Enter the Value of 6 Element: 456

 

List:  [123, 524, 654, 458, 253, 456]

 

The Second Largest Element in the Given List is:  524

Program 3: Python Program to find the Second Largest Number from a List Using set() and max() Function

In this program, we will use the set() function to get the unique element from the list to a new list. And max() to get the largest element from the list.

Python Program to find the Second Largest Number from a List Using set() and max() Function
# Python Program to find the Second Largest Number from a List Using set() and max() Function

NumList = []

# To Take the Input From the User
Number = int(input("Enter the Total Number of List Elements: "))
for i in range(1, Number + 1):
    value = int(input("Enter the Value of %d Element: " %i))
    NumList.append(value)
    
# Print the List Entered By the User
print("\nList: ", NumList)    

# To get unique elements
new_list = set(NumList)

# Removing the largest element from list1
new_list.remove(max(new_list))

# Now computing the max element by built-in method
print("\nThe Second Largest Element in the Given List is: ", max(new_list))
Output

Enter the Total Number of List Elements: 6

Enter the Value of 1 Element: 524

Enter the Value of 2 Element: 653

Enter the Value of 3 Element: 422

Enter the Value of 4 Element: 685

Enter the Value of 5 Element: 463

Enter the Value of 6 Element: 659

 

List:  [524, 653, 422, 685, 463, 659]

 

The Second Largest Element in the Given List is:  659

Program 4: Python Program to find the Second Largest Number from a List using max() and min() function

In this python program, we will use the Python built-in function max() and min(). max() function is used to get the largest element of a list and min() function is used to get the smallest element of a list.

Python Program to find the Second Largest Number from a List using max() and min() function

# Python Program to find the Second Largest Number from a List using max() and min() function

NumList = []

# To Take the Input From the User
Number = int(input("Enter the Total Number of List Elements: "))
for i in range(1, Number + 1):
    value = int(input("Enter the Value of %d Element: " %i))
    NumList.append(value)
    
# Print the List Entered By the User
print("\nList: ", NumList)    
    
# Assuming max_ is equal to maximum of element at 0th and 1st index
# and secondmax is the minimum among them
max_=max(NumList[0],NumList[1])
secondmax=min(NumList[0],NumList[1])
for i in range(2,len(NumList)):
   # if found element is greater than max_
   if NumList[i]>max_:
      secondmax=max_
      max_=NumList[i]
   #if found element is greator than secondmax
   else:
      if NumList[i]>secondmax:
         secondmax=NumList[i]
print("\nThe Second Largest number in the Given list is: ",str(secondmax))
Output

Enter the Total Number of List Elements: 6

Enter the Value of 1 Element: 456

Enter the Value of 2 Element: 23

Enter the Value of 3 Element: 654

Enter the Value of 4 Element: 52

Enter the Value of 5 Element: 566

Enter the Value of 6 Element: 245

 

List:  [456, 23, 654, 52, 566, 245]

 

The Second Largest number in the Given list is:  566

Program 5: Python Program to find the Second Largest Number from a List Without Using any built-in Function

In this program, we are not using any python built-in function.

Python Program to find the Second Largest Number from a List Without Using any built-in Function

# Python Program to find the Second Largest Number from a List Without Using any built-in Function

NumList = []

# take the Input From the User
Number = int(input("Enter the Total Number of List Elements: "))

for i in range(1, Number + 1):
    value = int(input("Enter the Value of %d Element: " %i))
    NumList.append(value)
    
# Print the List Entered By the User
print("\nList: ", NumList)    

first = second = NumList[0]
for j in range(1, Number):
    if(NumList[j] > first):
        second = first
        first = NumList[j]
    elif(NumList[j] > second and NumList[j] < first):
        second = NumList[j]
        

print("\nThe Largest Element in this List is: ", first)
print("The Second Largest Element in this List is: ", second)
Output

Enter the Total Number of List Elements: 6

Enter the Value of 1 Element: 463

Enter the Value of 2 Element: 523

Enter the Value of 3 Element: 452

Enter the Value of 4 Element: 895

Enter the Value of 5 Element: 546

Enter the Value of 6 Element: 745

 

List:  [463, 523, 452, 895, 546, 745]

 

The Largest Element in this List is:  895

The Second Largest Element in this List is:  745


Related Posts



Comments

Recent Posts
Tags