Second Largest Number in Python

Second Largest Number in Python

When we have a lot of elements in our list, the thought of finding the highest or lowest element can come to our mind and Python has made it much easier for us.

In this article, we shall how we can use to find the second largest number in Python from a list.

  1. Sorting the list and then print the second last number.
  2. Removing the maximum element.
  3. Finding the maximum element.
  4. Traversing the list.

Let us have a look at the first approach-

Sorting the list and then print the second last number

The following program illustrates how we can do it in Python-

Example –

Output:

The second largest element of the list is: 30

It’s time to go for the explanation part-

  1. We have declared the list from which we want to take out the second last element.
  2. After this, we used the sort method so that all the elements of our list are arranged in ascending order.
  3. Now we make use of negative indexing since the second-largest number will come at the second last position.

The second method is to obtain the second largest element of the list by removing the maximum element.

Let us see how we can do it.

Removing the maximum element

Example –

Output:

30

Explanation –

Let us understand what we have done in the above program-

  1. We have declared the list from which we want to take out the second last element.
  2. After this, we used the set method to take all the unique elements of the list.
  3. Now we make use of max() to get the maximum value from the list and then remove it.
  4. After this, we print the maximum of the resultant list which will give us the second-largest number.

In the third method, we will use for loop and find the second largest number from the list.

Example –

Output:

Enter number of elements in list: 5

Enter the elements: 10

Enter the elements: 20

Enter the elements: 30

Enter the elements: 40

Enter the elements: 50
The second largest element is: 40

Explanation –

Let us have a glance at what we have done here-

  1. We have declared an empty list in which we will insert the elements.
  2. After this, we ask the user to provide us the number of elements we would like to add to our list.
  3. After this, we use the sort method so that all the elements of our list are arranged in ascending order.
  4. Now we make use of negative indexing since the second-largest number will come at the second last position.

Traversing the list

In the last program, we will traverse the list to find out the largest number and then make use of conditional statements to find the second largest number from the list.

The following program illustrates the same-

Example –

Output:

30

Explanation –

Let us understand what we have done in the above program-

  1. The first step is to create a function that checks the largest number from the list by traversing it.
  2. In the next for loop, we traverse the list again for finding the highest number but this time excludes the previous one since here our objective is to find the second largest function.
  3. Finally, we pass our list in the function.

So, in this article, we got the chance to think out of the box and discover some new ways to develop the logic for finding the second largest number in Python.


原创文章,作者:ItWorker,如若转载,请注明出处:https://blog.ytso.com/263649.html

(0)
上一篇 2022年5月30日
下一篇 2022年5月30日

相关推荐

发表回复

登录后才能评论