{"id":24729,"date":"2021-11-02T09:45:47","date_gmt":"2021-11-02T04:15:47","guid":{"rendered":"https:\/\/python-programs.com\/?p=24729"},"modified":"2021-11-05T17:22:53","modified_gmt":"2021-11-05T11:52:53","slug":"python-list-sort-method-with-examples","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-list-sort-method-with-examples\/","title":{"rendered":"Python List sort() Method with Examples"},"content":{"rendered":"

In the previous article, we have discussed Python List reverse() Method with Examples<\/a>
\nList in Python:<\/strong><\/p>\n

Lists in Python are mutable sequences. They are extremely similar to tuples, except they do not have immutability constraints. Lists are often used to store collections of homogeneous things, but there is nothing stopping you from storing collections of heterogeneous items as well.<\/p>\n

List sort() Method in Python:<\/strong><\/p>\n

The sort() method sorts the elements of a list in ascending or descending order.<\/p>\n

By default, the sort() method ascends the list.<\/p>\n

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

list.sort(key=...., reverse=....)<\/pre>\n

Alternatively, you can use Python’s built-in sorted() function to accomplish the same thing.<\/p>\n

sorted(list, key=...., reverse=....)<\/pre>\n

Note:<\/strong> The most basic distinction between sort() and sorted() is that sort() changes the list directly and returns no value, whereas sorted() does not change the list and returns the sorted list.<\/p>\n

Parameter Values of sort():<\/strong><\/p>\n

Sort() does not require any additional parameters by default. It does, however, have two optional parameters:<\/p>\n