What is the List sort() Method?
list sort() function is an in-built function in Python, that is used to sort the values of a list in ascending or descending order. By default it sorts values in ascending order. Python list sort time complexity is O(nlogn).
It is a very useful and simple list operation. It changes the original list instead of returning the new ones.
Python List sort() Method
Python list sort() method sorts the elements of a list. It sorts in ascending order by default but can also sort values in descending order or in a custom manner using its parameters.
Example: Python list sort alphabetically and numerically.
Python
alphabets = [ 'a' , 'e' , 'd' , 'c' , 'b' ] alphabets.sort() print (alphabets) random_numbers = [ 2 , 5 , 6 , 1 , 8 , 3 ] random_numbers.sort() print (random_numbers) |
Output:
['a', 'b', 'c', 'd', 'e']
[1, 2, 3, 5, 6, 8]