{"id":3027,"date":"2023-10-20T08:33:02","date_gmt":"2023-10-20T03:03:02","guid":{"rendered":"https:\/\/python-programs.com\/?p=3027"},"modified":"2023-11-10T11:51:01","modified_gmt":"2023-11-10T06:21:01","slug":"python-sort-a-list-of-numbers-in-ascending-or-descending-order-list-sort-vs-sorted","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-sort-a-list-of-numbers-in-ascending-or-descending-order-list-sort-vs-sorted\/","title":{"rendered":"Python : Sort a List of Numbers in Ascending or Descending Order | list.sort() vs sorted()"},"content":{"rendered":"
A list is used in Python to store the sequence of different types of data. Python lists are mutable, which means that their elements can be changed after they have been created. Python, on the other hand, has six data types that can be used to store sequences, with the first two on the list being the most common and accurate.<\/p>\n
A list is a collection of various types of values or objects. The list items are separated by a comma (,), which is enclosed in square brackets [].<\/p>\n
Example:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n Ascending order:<\/strong><\/p>\n Descending order:<\/strong><\/p>\n In this article, we’ll look at two methods for sorting a list of numbers in ascending and descending order.<\/p>\n It’s a built-in function that takes an iterable and returns a new sorted list from it. Let us the sort the list of integers in ascending order<\/p>\n Below is the implementation:<\/p>\n Output:<\/strong><\/p>\n Using reverse=True gives the list in descending order<\/p>\n Below is the implementation:<\/p>\n Output:<\/strong><\/p>\n The sorted() function returns a sorted list of the iterable object specified. You can specify whether the order should be ascending or descending. Numbers are sorted numerically, while strings are sorted alphabetically. It is important to note that you cannot sort a list that contains BOTH string and numeric values.<\/p>\n Characteristics of sorted() function:<\/strong><\/p>\n Below is the implemenation:<\/p>\n Output:<\/strong><\/p>\n Using reverse=True gives the list in descending order<\/p>\n Below is the implementation:<\/p>\n Output:<\/strong><\/p>\n <\/p>\ngivenlist = [3, 5, 1, 6, 9, 2, 8, 7]<\/pre>\n
[1, 2, 3, 5, 6, 7, 8, 9]<\/pre>\n
[9, 8, 7, 6, 5, 3, 2, 1]<\/pre>\n
Sort the given list<\/h2>\n
\n
Method #1: Using sort() function to sort in ascending order:<\/h3>\n
\nNote:<\/strong><\/p>\n\n
# Driver code\r\n# Given list\r\ngivenlist = [3, 5, 1, 6, 9, 2, 8, 7]\r\n# sorting the given list in ascending order\r\ngivenlist.sort()\r\nprint(givenlist)\r\n<\/pre>\n
[1, 2, 3, 5, 6, 7, 8, 9]<\/pre>\n
Method #2: Using sort() function to sort in descending order:<\/h3>\n
# Driver code\r\n# Given list\r\ngivenlist = [3, 5, 1, 6, 9, 2, 8, 7]\r\n# sorting the given list in descending order\r\ngivenlist.sort(reverse=True)\r\nprint(givenlist)\r\n<\/pre>\n
[9, 8, 7, 6, 5, 3, 2, 1]<\/pre>\n
Method #3: Using sorted() function to sort in ascending order<\/h3>\n
\n
# Driver code\r\n# Given list\r\ngivenlist = [3, 5, 1, 6, 9, 2, 8, 7]\r\n\r\n# using sorted function to sort the given list in ascending order\r\nnewlist = sorted(givenlist)\r\n# printing the newlist\r\nprint(newlist)\r\n<\/pre>\n
[1, 2, 3, 5, 6, 7, 8, 9]<\/pre>\n
Method #4: Using sorted() function to sort in descending order:<\/h3>\n
# Driver code\r\n# Given list\r\ngivenlist = [3, 5, 1, 6, 9, 2, 8, 7]\r\n\r\n# using sorted function to sort the given list in descending order using reverse=True\r\nnewlist = sorted(givenlist, reverse=True)\r\n# printing the newlist\r\nprint(newlist)\r\n<\/pre>\n
[9, 8, 7, 6, 5, 3, 2, 1]<\/pre>\n