How do I sort lists in Python in pure Python?

In Python, you can sort lists using the built-in sort() method or the sorted() function. The sort() method sorts the list in place and returns None, while sorted() returns a new sorted list from the elements of any iterable.

Here are some examples of how to use both methods:

# Example using sort() numbers = [5, 2, 9, 1, 5, 6] numbers.sort() print(numbers) # Output: [1, 2, 5, 5, 6, 9] # Example using sorted() numbers = [5, 2, 9, 1, 5, 6] sorted_numbers = sorted(numbers) print(sorted_numbers) # Output: [1, 2, 5, 5, 6, 9]

sort sorted Python lists sorting algorithms Python