Understanding the Difference between sort() and sorted() in Python

In Python, sorting lists and other iterable types is a common operation. Two primary tools for this are the sort() method and the sorted() function. While they might seem similar at first glance, they have distinct differences and use cases. This blog post aims to demystify these differences, helping Python programmers choose the right tool for their sorting needs.

The sort() Method

link to this section

In-Place Sorting

The sort() method is a list method that modifies the list it is called on. This means that it does not return a new list; instead, it rearranges the elements of the original list into sorted order. This is known as in-place sorting.

Example:

my_list = [3, 1, 4, 1, 5, 9, 2] 
my_list.sort() 
print(my_list) # Output: [1, 1, 2, 3, 4, 5, 9] 

Key Features of sort()

  • Modifies the original list : No new list is created, which can be more memory efficient for large lists.
  • Only for lists : sort() is a list method and thus, cannot be used with other iterables.
  • Custom sorting : Supports a key parameter to specify a function to be called on each list element prior to making comparisons.

The sorted() Function

link to this section

Creating a New Sorted List

Unlike sort() , sorted() works on any iterable, not just lists, and returns a new list containing all the elements of the iterable in sorted order. The original iterable remains unchanged.

Example:

my_tuple = (3, 1, 4, 1, 5, 9, 2) 
sorted_tuple = sorted(my_tuple) 
print(sorted_tuple) # Output: [1, 1, 2, 3, 4, 5, 9] 
print(my_tuple) # Original tuple remains unchanged 

Key Features of sorted()

  • Works with any iterable : Can be used with lists, tuples, dictionaries, and more.
  • Returns a new sorted list : Does not alter the original data.
  • Supports custom sorting : Like sort() , it accepts a key function for custom sorting criteria.

Comparing sort() and sorted()

link to this section
Aspect sort() sorted()
Data Type Only for lists. Works with any iterable (lists, tuples, strings, etc.).
Return Type None. Modifies the list in place. Returns a new list. The original iterable remains unchanged.
Memory Efficiency More efficient as it doesn't create a new list. Less memory efficient as it creates a new list.
Use Case When you want to modify the original list. When you need to maintain the original data and require a sorted version of it.

Best Practices

link to this section
  • Choose sort() for large lists : If you are working with a large list and need to sort it, sort() is generally more memory efficient.
  • Use sorted() for any iterable : When working with tuples, strings, or when you need to preserve the original iterable, use sorted() .
  • Readability and Intent : Choose the method that best communicates the intent of your code. sorted() makes it clear that the original data remains unchanged.

Conclusion

link to this section

The choice between sort() and sorted() in Python should be guided by the nature of your data and what you intend to do with it post-sorting. If you're working with a list and want to sort it directly, sort() is the way to go. If you're dealing with other iterables, or if you want to keep the original order intact, then sorted() is your tool of choice. Understanding these subtleties is crucial for writing efficient, clear, and effective Python code.