{"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

given nested list =[['hello', 11], ['this', 1], ['is', 23], ['btechgeeks', 19], ['online', 39], ['platform', 7], ['for', 29]]<\/pre>\n

Output:<\/strong><\/p>\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

Example2:<\/strong><\/p>\n

Input:<\/strong><\/p>\n

[['hello', 46], ['this', 31], ['morning', 29], ['is', 26], ['btechGeeks', 19], ['online', 33]]<\/pre>\n

Output:<\/strong><\/p>\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

Example3:<\/strong><\/p>\n

Input:<\/strong><\/p>\n

[['sky', 12], ['is', 39], ['blue', 5]]<\/pre>\n

Output:<\/strong><\/p>\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

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