Python sort() Syntax
The syntax of the sort() function in Python is as follows.
Syntax: list_name.sort(key=…, reverse=…)
Parameters:
By default, Python sort() doesn’t require any extra parameters and sorts the list in ascending order. However, it has two optional parameters:
- key: function that serves as a key for the sort comparison
- reverse: If true, the list is sorted in descending order.
Return value: The sort() does not return anything but alters the original list according to the passed parameter.
sort() in Python
The sort function can be used to sort the list in both ascending and descending order. It can be used to sort lists of integers, floating point numbers, strings, and others in Python. Its time complexity is O(NlogN).