How to Sort a List

To sort a list in ascending or descending order, you can use the sort() method. By default, the list is sorted in ascending order.

Example: Sorting a List

# Sorting a list in ascending order
numbers = [5, 3, 8, 1]
numbers.sort()
print(numbers)

Output

[1, 3, 5, 8]

Explanation: The sort() method sorts the elements in the list in ascending order.