Blog Post #52: Organizing Your Data: Sorting Lists with sort() and sorted()

In Post #50 and Post #51, we learned how to change lists by adding and removing items. Another common and crucial operation is to organize the data within a list by sorting it, for example, from lowest to highest or in alphabetical order.

Python provides two ways to do this: the .sort() method and the sorted() function. They look similar, but they behave in fundamentally different ways. Understanding this difference is key to avoiding common bugs.

Method 1: The .sort() Method (In-Place Sorting)

The .sort() method sorts a list in-place. This means it modifies the original list directly and does not create a new one.

Because it modifies the list in-place, the .sort() method returns None. A very common beginner mistake is to write my_new_list = my_list.sort(), which will result in my_new_list being None, not the sorted list you expected.

Let’s see it in action:

numbers = [4, 1, 7, 3, 9]
print(f"Original list: {numbers}")

# The .sort() method modifies the 'numbers' list directly.
# It does not return a new list.
numbers.sort()

print(f"List after .sort(): {numbers}")

The output shows the original list has been changed:

Original list: [4, 1, 7, 3, 9]
List after .sort(): [1, 3, 4, 7, 9]

Sorting in Reverse

To sort in descending order (from highest to lowest), you can pass the argument reverse=True to the method.

numbers.sort(reverse=True)
print(f"Sorted in descending order: {numbers}") # Output: [9, 7, 4, 3, 1]

Method 2: The sorted() Function (Returning a New List)

The sorted() function, on the other hand, is a general-purpose function that takes a list (or any other iterable) as an argument and returns a brand new, sorted list, leaving the original list completely unchanged.

This is useful when you want to preserve the original order of your data but also need to work with a sorted version of it.

scores = [88, 54, 92, 78]
print(f"Original scores: {scores}")

# sorted() returns a new list, which we store in a new variable.
sorted_scores = sorted(scores)

print(f"The new sorted list is: {sorted_scores}")
print(f"The original scores list is still the same: {scores}")

The output demonstrates that the original list is preserved:

Original scores: [88, 54, 92, 78]
The new sorted list is: [54, 78, 88, 92]
The original scores list is still the same: [88, 54, 92, 78]

Like the .sort() method, the sorted() function also accepts the reverse=True argument.

Summary: Which One Should You Use?

Here’s a simple guide to help you choose:

Use the .sort() method when:

  • You want to permanently change the order of the original list.
  • You don’t need to preserve the original order.
  • You are modifying your own list inside a contained piece of logic.

Use the sorted() function when:

  • You need to keep the original list in its original order.
  • You want to create a new, sorted copy to work with.

Rule of Thumb: When in doubt, the sorted() function is often a safer choice because it doesn’t alter your original data, which can help prevent unexpected side effects in your program.

What’s Next?

You can now confidently sort your list data in both ascending and descending order. More importantly, you understand the critical difference between the in-place .sort() method and the sorted() function that returns a new list.

Beyond adding, removing, and sorting, lists have several other handy methods for finding items and getting information about their contents. In Post #53, we will explore some of these essential utility methods, including .index(), .count(), and .reverse().

Author

Debjeet Bhowmik

Experienced Cloud & DevOps Engineer with hands-on experience in AWS, GCP, Terraform, Ansible, ELK, Docker, Git, GitLab, Python, PowerShell, Shell, and theoretical knowledge on Azure, Kubernetes & Jenkins. In my free time, I write blogs on ckdbtech.com

Leave a Comment