Sorting is a common task in programming. Whether you want to put numbers in order or tidy up a list of names, Python gives you simple tools to do it. Let’s look at how it works.
Using sort()
The sort() method arranges the items in a list into order. By default, it sorts in ascending order — that means from smallest to largest for numbers, and alphabetically for text.
numbers = [5, 2, 9, 1, 7]
numbers.sort()
print(numbers)
# [1, 2, 5, 7, 9]
This changes the original list. The items are now permanently in order.
Things to try:
- Create a list of five numbers in random order and sort them.
- Make a list of names and use
sort()to arrange them alphabetically.
Sorting in Reverse
If you’d like to sort a list in the opposite direction (descending order), you can use the reverse=True option inside sort().
numbers = [5, 2, 9, 1, 7]
numbers.sort(reverse=True)
print(numbers)
# [9, 7, 5, 2, 1]
This is useful when you want the biggest numbers or the last letters first.
Things to try:
- Sort a list of numbers in descending order.
- Sort a list of words so they appear from Z to A.
Using sorted()
The sorted() function looks very similar to sort(), but there’s one key difference: it does not change the original list. Instead, it creates a brand-new sorted version while leaving the original untouched.
numbers = [5, 2, 9, 1, 7]
new_list = sorted(numbers)
print(new_list)
# [1, 2, 5, 7, 9]
print(numbers)
# [5, 2, 9, 1, 7]
This is handy when you want both the original and the sorted list available. Like sort(), you can also add reverse=True to flip the order.
new_list = sorted(numbers, reverse=True)
print(new_list)
# [9, 7, 5, 2, 1]
Things to try:
- Use
sorted()on a list of numbers, then check that the original list hasn’t changed. - Try sorting the same list both forwards and backwards using
sorted().
Wrapping Up
The main difference to remember is that sort() changes your list directly, while sorted() gives you a new one. Both are useful, depending on whether you want to keep the original order or not. Once you’re comfortable with these tools, you’ll find sorting lists becomes second nature.





