{"id":9531,"date":"2021-10-01T10:00:56","date_gmt":"2021-10-01T04:30:56","guid":{"rendered":"https:\/\/python-programs.com\/?p=9531"},"modified":"2021-11-22T18:33:29","modified_gmt":"2021-11-22T13:03:29","slug":"python-program-to-sort-the-list-according-to-the-second-element-in-sublist","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-program-to-sort-the-list-according-to-the-second-element-in-sublist\/","title":{"rendered":"Python Program to Sort the List According to the Second Element in Sublist"},"content":{"rendered":"
Have you mastered basic programming topics of java and looking forward to mastering advanced topics in a java programming language? Go with these ultimate Advanced java programs examples with output<\/a> & achieve your goal in improving java coding skills.<\/p>\n Given list of lists which contain two elements in every sub list , the task is to sort the lists according to the second element in sub 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 Example3:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n Below are the ways to sort the given list of lists according to the second element in sub list in python some of them are:<\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Explanation:<\/strong><\/p>\n Related Programs<\/strong>:<\/p>\n Have you mastered basic programming topics of java and looking forward to mastering advanced topics in a java programming language? Go with these ultimate Advanced java programs examples with output & achieve your goal in improving java coding skills. Given list of lists which contain two elements in every sub list , the task is …<\/p>\ngiven nested list =[['hello', 11], ['this', 1], ['is', 23], ['btechgeeks', 19], ['online', 39], ['platform', 7], ['for', 29]]<\/pre>\n
printing the sorted nested list before sorting :\r\n[['hello', 11], ['this', 1], ['is', 23], ['btechgeeks', 19], ['online', 39], ['platform', 7], ['for', 29]]\r\nprinting the sorted nested list after sorting :\r\n[['this', 1], ['platform', 7], ['hello', 11], ['btechgeeks', 19], ['is', 23], ['for', 29], ['online', 39]]<\/pre>\n
[['hello', 46], ['this', 31], ['morning', 29], ['is', 26], ['btechGeeks', 19], ['online', 33]]<\/pre>\n
printing the sorted nested list before sorting :\r\n[['hello', 46], ['this', 31], ['morning', 29], ['is', 26], ['btechGeeks', 19], ['online', 33]]\r\nprinting the sorted nested list after sorting :\r\n[['btechGeeks', 19], ['is', 26], ['morning', 29], ['this', 31], ['online', 33], ['hello', 46]]<\/pre>\n
[['sky', 12], ['is', 39], ['blue', 5]]<\/pre>\n
printing the sorted nested list before sorting :\r\n[['sky', 12], ['is', 39], ['blue', 5]]\r\nprinting the sorted nested list after sorting :\r\n[['blue', 5], ['sky', 12], ['is', 39]]<\/pre>\n
Program to Sort the List According to the Second Element in Sub list in Python<\/h2>\n
\n
Method #1: Using nested loops and temporary variable(Static Input)<\/h3>\n
\n
# Take in a list that contains sublists as static input.\r\nnestedList = [['hello', 46], ['this', 31], ['morning', 29],\r\n ['is', 26], ['btechGeeks', 19], ['online', 33]]\r\n# printing the sorted nested list before sorting\r\nprint('printing the sorted nested list before sorting :')\r\nprint(nestedList)\r\n# Using two for loops, sort the sublists using bubble\r\n# sort based on the second value of the sublist.\r\nfor m in range(len(nestedList)):\r\n for n in range(len(nestedList)-m-1):\r\n # If the second element of the first sublist is greater than the second element of the second sublist,\r\n # the complete sublist should be exchanged\/swapped using temporary variable\r\n if(nestedList[n][1] > nestedList[n+1][1]):\r\n tempo = nestedList[n]\r\n nestedList[n] = nestedList[n+1]\r\n nestedList[n+1] = tempo\r\n# printing the sorted nested list after sorting\r\nprint('printing the sorted nested list after sorting :')\r\nprint(nestedList)\r\n<\/pre>\n
printing the sorted nested list before sorting :\r\n[['hello', 46], ['this', 31], ['morning', 29], ['is', 26], ['btechGeeks', 19], ['online', 33]]\r\nprinting the sorted nested list after sorting :\r\n[['btechGeeks', 19], ['is', 26], ['morning', 29], ['this', 31], ['online', 33], ['hello', 46]]<\/pre>\n
Method #2: Using nested loops and temporary variable(User Input)<\/h3>\n
\n
# Take a empty list.\r\nnestedList = []\r\n# Scan the number of elements \/number of sublists as user input using int(input()) function.\r\ntotalsublists = int(input('Enter some random number of sublists ='))\r\n# Loop from 1 to given number of elements times using for loop.\r\nfor r in range(totalsublists):\r\n # Scan the list elements using input() and int(input()) functions and append\r\n # to the empty list using append() function.\r\n firsteleme = input('Enter some random first element =')\r\n secondeleme = int(input('Enter some random second element ='))\r\n nestedList.append([firsteleme, secondeleme])\r\n\r\n# printing the sorted nested list before sorting\r\nprint('printing the sorted nested list before sorting :')\r\nprint(nestedList)\r\n# Using two for loops, sort the sublists using bubble\r\n# sort based on the second value of the sublist.\r\nfor m in range(len(nestedList)):\r\n for n in range(len(nestedList)-m-1):\r\n # If the second element of the first sublist is greater than the second element of the second sublist,\r\n # the complete sublist should be exchanged\/swapped using temporary variable\r\n if(nestedList[n][1] > nestedList[n+1][1]):\r\n tempo = nestedList[n]\r\n nestedList[n] = nestedList[n+1]\r\n nestedList[n+1] = tempo\r\n# printing the sorted nested list after sorting\r\nprint('printing the sorted nested list after sorting :')\r\nprint(nestedList)\r\n<\/pre>\n
Enter some random number of sublists =7\r\nEnter some random first element =hello\r\nEnter some random second element =11\r\nEnter some random first element =this\r\nEnter some random second element =1\r\nEnter some random first element =is\r\nEnter some random second element =23\r\nEnter some random first element =btechgeeks\r\nEnter some random second element =19\r\nEnter some random first element =online\r\nEnter some random second element =39\r\nEnter some random first element =platform\r\nEnter some random second element =7\r\nEnter some random first element =for\r\nEnter some random second element =29\r\nprinting the sorted nested list before sorting :\r\n[['hello', 11], ['this', 1], ['is', 23], ['btechgeeks', 19], ['online', 39], ['platform', 7], ['for', 29]]\r\nprinting the sorted nested list after sorting :\r\n[['this', 1], ['platform', 7], ['hello', 11], ['btechgeeks', 19], ['is', 23], ['for', 29], ['online', 39]]\r\n\r\n<\/pre>\n
\n
\n