How to Use List sort() Function
Using the list sort() function in Python is very easy. You just need to call the function with the list object. You can use the parameters if you want but it is not mandatory to use.
Note: The difference between sort() and sorted() is that the sort list in Python alters the list directly and produces no output, whereas sorted() doesn’t change the list and returns the sorted list.
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]
Contact Us