{"id":8822,"date":"2023-11-04T11:11:43","date_gmt":"2023-11-04T05:41:43","guid":{"rendered":"https:\/\/python-programs.com\/?p=8822"},"modified":"2023-11-10T12:16:09","modified_gmt":"2023-11-10T06:46:09","slug":"python-program-to-sort-a-list-of-tuples-in-increasing-order-by-the-last-element-in-each-tuple","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-program-to-sort-a-list-of-tuples-in-increasing-order-by-the-last-element-in-each-tuple\/","title":{"rendered":"Python Program to Sort a List of Tuples in Increasing Order by the Last Element in Each Tuple"},"content":{"rendered":"
Tuple in Python:<\/strong><\/p>\n Tuples, like Python lists, are a common data type that allows you to store values in a series. They could be handy in cases when you want to communicate data with someone but not enable them to change it. They can use the data values, but no change is reflected in the original data that was supplied.<\/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 There are several ways to sort a List of Tuples in ascending order by the last element in each tuple some of them are:<\/p>\n Drive into Python Programming Examples<\/a> and explore more instances related to python concepts so that you can become proficient in generating programs in Python Programming Language.<\/p>\n The Sorted()<\/strong> method sorts a list and always returns a list with the entries sorted without changing the original sequence.<\/p>\n i)List of tuples of Integer type<\/strong><\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n ii<\/strong>)List of tuples of String type<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n The sort() method arranges the items in a list in an ascending or descending order.<\/p>\n We use lambda function to get the last element in the given list of tuples.<\/p>\n i)List of tuples of Integer type<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n ii)List of tuples of String type<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Related Programs<\/strong>:<\/p>\n Tuple in Python: Tuples, like Python lists, are a common data type that allows you to store values in a series. They could be handy in cases when you want to communicate data with someone but not enable them to change it. They can use the data values, but no change is reflected in the …<\/p>\n\u00a0given list of tuples =[(5, 12, 98), (7, 1), (4, 19, 11, 9), (36, 82, 19, 1, 2, 5, 3, 6, 9, 6)]<\/pre>\n
Printing the sorted list of tuples : \r\n[(7, 1), (36, 82, 19, 1, 2, 5, 3, 6, 9, 6), (4, 19, 11, 9), (5, 12, 98)]<\/pre>\n
\u00a0given list of tuples = [(7, 12, 23), (999, 4), (234, 245, 129), (10, 23, 456)]<\/pre>\n
Printing the sorted list of tuples : \r\n[(999, 4), (7, 12, 23), (234, 245, 129), (10, 23, 456)]<\/pre>\n
\u00a0given list of tuples = [('hello', 'this'), ('BTechGeeks', 'online', 'platform'), ('for', 'students')]<\/pre>\n
Printing the sorted list of tuples : \r\n[('BTechGeeks', 'online', 'platform'), ('for', 'students'), ('hello', 'this')]<\/pre>\n
Program to Sort a List of Tuples in Increasing Order by the Last Element in Each Tuple<\/h2>\n
\n
Method #1:Using sorted function<\/h3>\n
\n
def lastEle(ele):\r\n # returning the last element\r\n return ele[-1]\r\n\r\n\r\ndef sortlastElementTuple(listTuple):\r\n # using sorted function with first parameter as given list of tuples and\r\n # key as last element\r\n # To get last element we create a function which returns the last element\r\n # returning the sorted list of tuples\r\n return sorted(listTuple, key=lastEle)\r\n\r\n\r\nlistofTuples = [(5, 12, 98), (7, 1), (4, 19, 11, 9),\r\n (36, 82, 19, 1, 2, 5, 3, 6, 9, 6)]\r\n# printing the sorted list of tuples by last element\r\nprint(\"Printing the sorted list of tuples : \")\r\n# Passing the given list of tuples to sortlastElementTuple function\r\nprint(sortlastElementTuple(listofTuples))\r\n<\/pre>\n
Printing the sorted list of tuples : \r\n[(7, 1), (36, 82, 19, 1, 2, 5, 3, 6, 9, 6), (4, 19, 11, 9), (5, 12, 98)]<\/pre>\n
def lastEle(ele):\r\n # returning the last element\r\n return ele[-1]\r\n\r\n\r\ndef sortlastElementTuple(listTuple):\r\n # using sorted function with first parameter as given list of tuples and\r\n # key as last element\r\n # To get last element we create a function which returns the last element\r\n # returning the sorted list of tuples\r\n return sorted(listTuple, key=lastEle)\r\n\r\n\r\nlistofTuples = [('hello', 'this'), ('BTechGeeks', 'online',\r\n 'platform'), ('for', 'students')]\r\n# printing the sorted list of tuples by last element\r\nprint(\"Printing the sorted list of tuples : \")\r\n# Passing the given list of tuples to sortlastElementTuple function\r\nprint(sortlastElementTuple(listofTuples))\r\n<\/pre>\n
Printing the sorted list of tuples : \r\n[('BTechGeeks', 'online', 'platform'), ('for', 'students'), ('hello', 'this')]<\/pre>\n
Method #2:Using sort function<\/h3>\n
def sortlastElementTuple(listTuple):\r\n # The key has been configured to sort using the\r\n # last element of the sublist lambda has been used\r\n listTuple.sort(key=lambda k: k[-1])\r\n # returning the sorted list of tuples\r\n return listTuple\r\n\r\n\r\nlistofTuples = [(5, 12, 98), (7, 1), (4, 19, 11, 9),\r\n (36, 82, 19, 1, 2, 5, 3, 6, 9, 6)]\r\n# printing the sorted list of tuples by last element\r\nprint(\"Printing the sorted list of tuples : \")\r\n# Passing the given list of tuples to sortlastElementTuple function\r\nprint(sortlastElementTuple(listofTuples))\r\n<\/pre>\n
Printing the sorted list of tuples : \r\n[(7, 1), (36, 82, 19, 1, 2, 5, 3, 6, 9, 6), (4, 19, 11, 9), (5, 12, 98)]<\/pre>\n
def sortlastElementTuple(listTuple):\r\n # The key has been configured to sort using the\r\n # last element of the sublist lambda has been used\r\n listTuple.sort(key=lambda k: k[-1])\r\n # returning the sorted list of tuples\r\n return listTuple\r\n\r\n\r\nlistofTuples = [('hello', 'this'), ('BTechGeeks', 'online',\r\n 'platform'), ('for', 'students')]\r\n# printing the sorted list of tuples by last element\r\nprint(\"Printing the sorted list of tuples : \")\r\n# Passing the given list of tuples to sortlastElementTuple function\r\nprint(sortlastElementTuple(listofTuples))\r\n<\/pre>\n
Printing the sorted list of tuples : \r\n[('BTechGeeks', 'online', 'platform'), ('for', 'students'), ('hello', 'this')]<\/pre>\n
\n