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

givenlist = [3, 5, 1, 6, 9, 2, 8, 7]<\/pre>\n

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

Ascending order:<\/strong><\/p>\n

[1, 2, 3, 5, 6, 7, 8, 9]<\/pre>\n

Descending order:<\/strong><\/p>\n

[9, 8, 7, 6, 5, 3, 2, 1]<\/pre>\n

Sort the given list<\/h2>\n

In this article, we’ll look at two methods for sorting a list of numbers in ascending and descending order.<\/p>\n