{"id":8485,"date":"2021-09-30T16:00:21","date_gmt":"2021-09-30T10:30:21","guid":{"rendered":"https:\/\/python-programs.com\/?p=8485"},"modified":"2021-11-22T18:34:25","modified_gmt":"2021-11-22T13:04:25","slug":"python-program-to-find-the-second-largest-number-in-a-list","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-program-to-find-the-second-largest-number-in-a-list\/","title":{"rendered":"Python Program to Find the Second Largest Number in a List"},"content":{"rendered":"
The best and excellent way to learn a java programming language is by practicing Simple Java Program Examples<\/a> as it includes basic to advanced levels of concepts.<\/p>\n Lists in Python:<\/strong><\/p>\n 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.<\/p>\n 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?<\/p>\n 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.<\/p>\n Given a list , the task is to print the second largest Number in the given array\/list<\/p>\n Examples:<\/strong><\/p>\n Example1:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n Example2:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n There are several ways to print the second largest element in the\u00a0 given list some of them are:<\/p>\n Explore more instances related to python concepts from\u00a0Python Programming Examples<\/a>\u00a0Guide and get promoted from beginner to professional programmer level in Python Programming Language.<\/p>\n 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<\/p>\n 1)By sorting the given list in ascending order<\/strong><\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n 2)By sorting the given list in descending order<\/strong><\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Note : If there are duplicate values, this technique does not print the right output (specially two largest numbers)<\/p>\n Example:<\/strong><\/p>\n given list = [5, 6, 7, 7, 8, 1, 9, 9]<\/p>\n When we sort the given list in descending order, it appears like this:<\/p>\n given list =[ 9 , 9, 8 , 7 , 7, ,6 , 5 ,1 ]<\/p>\n When we print the second element, it prints 9, although the correct output is 8.<\/p>\n To solve this, we use sets to eliminate duplicates.<\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Related Programs<\/strong>:<\/p>\n 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 …<\/p>\ngiven list = [8, 12, 4, 6, 7, 3, 2, 6, 1, 9]<\/pre>\n
printing the second largest element in the given list : 9<\/pre>\n
given list = [5, 6, 7, 7, 8, 1, 9, 9]<\/pre>\n
printing the second largest element in the given list : 8<\/pre>\n
Program to Find the Second Largest Number in a List in Python<\/h2>\n
\n
Method #1: By sorting the given list<\/h3>\n
\n
# given list\r\ngiven_list = [8, 12, 4, 6, 7, 3, 2, 6, 1, 9]\r\n# sorting the given list in ascending order\r\ngiven_list.sort()\r\n# Calculating the second largest element after sorting\r\nseclarg = given_list[-2]\r\n# printing the second largest element in the given list\r\nprint(\"printing the second largest element in the given list : \", seclarg)\r\n<\/pre>\n
printing the second largest element in the given list : 9<\/pre>\n
\n
# given list\r\ngiven_list = [8, 12, 4, 6, 7, 3, 2, 6, 1, 9]\r\n# sorting the given list in ascending order\r\ngiven_list.sort(reverse=True)\r\n# Calculating the second largest element after sorting\r\nseclarg = given_list[1]\r\n# printing the second largest element in the given list\r\nprint(\"printing the second largest element in the given list : \", seclarg)\r\n<\/pre>\n
Method #2:Using sets<\/h3>\n
\n
# given list\r\ngiven_list = [5, 6, 7, 7, 8, 1, 9, 9]\r\n# converting the given list into set using set() function\r\ngiven_list = set(given_list)\r\n# converting the given_list set to list using list() function\r\ngiven_list = list(given_list)\r\n# sorting the given list in ascending order\r\ngiven_list.sort(reverse=True)\r\n# Calculating the second largest element after sorting\r\nseclarg = given_list[1]\r\n# printing the second largest element in the given list\r\nprint(\"printing the second largest element in the given list : \", seclarg)\r\n<\/pre>\n
printing the second largest element in the given list : 8<\/pre>\n
\n