Python Program to Find the Second Largest Number in a List

The best and excellent way to learn a java programming language is by practicing Simple Java Program Examples as it includes basic to advanced levels of concepts.

Lists in Python:

We’ve learnt a lot of things so far, from printing something to making a decision to iterating with loops. This and the following chapters will be about storing data. So, let’s begin with a list, which is used to hold a collection of facts.

In our program, we frequently require a list. Assume you’re creating a software to save the grades of all 50 students in a class. Considering 50 distinct factors is not a smart idea. Instead, we can store all 50 values (marks) in a single variable as a list. Isn’t it cool?

The Python list is a straightforward ordered list of items. Python lists are extremely powerful since the objects in them do not have to be homogeneous or even of the same type. Strings, numbers, and characters, as well as other lists, can all be found in a Python list. Python lists are also mutable: once stated, they can be easily altered via a variety of list methods. Let’s look at how to use these techniques to handle Python lists.

Given a list , the task is to print the second largest Number in the given array/list

Examples:

Example1:

Input:

given list = [8, 12, 4, 6, 7, 3, 2, 6, 1, 9]

Output:

printing the second largest element in the given list :  9

Example2:

Input:

given list = [5, 6, 7, 7, 8, 1, 9, 9]

Output:

printing the second largest element in the given list :  8

Program to Find the Second Largest Number in a List in Python

There are several ways to print the second largest element in the  given list some of them are:

Explore more instances related to python concepts from Python Programming Examples Guide and get promoted from beginner to professional programmer level in Python Programming Language.

Method #1: By sorting the given list

We can get the second largest number of the given list by sorting it in =and printing the second largest number in the given list

1)By sorting the given list in ascending order

Approach:

  • Scan the given list or give list as static input.
  • Sort the given list using sort() function in ascending order.
  • Print the second number from the end of the list.
  • End of program.

Below is the implementation:

# given list
given_list = [8, 12, 4, 6, 7, 3, 2, 6, 1, 9]
# sorting the given list in ascending order
given_list.sort()
# Calculating the second largest element after sorting
seclarg = given_list[-2]
# printing the second largest element in the given list
print("printing the second largest element in the given list : ", seclarg)

Output:

printing the second largest element in the given list :  9

2)By sorting the given list in descending order

Approach:

  • Scan the given list or give list as static input.
  • Sort the given list using sort() function and (reverse=True) in descending order.
  • Print the second number from the start number of the list.
  • End of program.

Below is the implementation:

# given list
given_list = [8, 12, 4, 6, 7, 3, 2, 6, 1, 9]
# sorting the given list in ascending order
given_list.sort(reverse=True)
# Calculating the second largest element after sorting
seclarg = given_list[1]
# printing the second largest element in the given list
print("printing the second largest element in the given list : ", seclarg)

Note : If there are duplicate values, this technique does not print the right output (specially two largest numbers)

Example:

given list = [5, 6, 7, 7, 8, 1, 9, 9]

When we sort the given list in descending order, it appears like this:

given list =[ 9 , 9, 8 , 7 , 7, ,6 , 5 ,1 ]

When we print the second element, it prints 9, although the correct output is 8.

To solve this, we use sets to eliminate duplicates.

Method #2:Using sets

Approach:

  • First we convert the given list to set using set() function.
  • Then we convert the given set to list using list() function because we cannot apply sort function in set.
  • Sort this list using sort() function and (reverse=True) in descending order.
  • Print the second number from the start number of the list.
  • End of program.

Below is the implementation:

# given list
given_list = [5, 6, 7, 7, 8, 1, 9, 9]
# converting the given list into set using set() function
given_list = set(given_list)
# converting the given_list set to list using list() function
given_list = list(given_list)
# sorting the given list in ascending order
given_list.sort(reverse=True)
# Calculating the second largest element after sorting
seclarg = given_list[1]
# printing the second largest element in the given list
print("printing the second largest element in the given list : ", seclarg)

Output:

printing the second largest element in the given list :  8

Related Programs: