Last Updated: January 3, 2026
Sorting and reversing data are fundamental operations in programming that can have a significant impact on how we interact with and analyze collections of information.
In Python, the built-in functions sorted() and reversed() provide elegant and efficient ways to tackle these tasks. Let's dive into what these functions offer and how you can use them effectively in your projects.
sorted()The sorted() function is a powerful tool that returns a new list containing all items from an iterable in ascending order. Its simplicity makes it a go-to for many developers, but there are some nuances to understand.
The most straightforward use of sorted() is simply passing in an iterable, like a list. The function returns a new sorted list without altering the original.
Here, you see that sorted() returns a new list called sorted_numbers, while the original numbers list remains unchanged. This immutability is key when you want to keep the original data intact for other operations.
By default, sorted() arranges items in ascending order. If you want to sort in descending order, use the reverse parameter.
This change is straightforward: just set reverse=True to flip the sort order.
keyOne of the most powerful features of sorted() is the ability to sort with a custom key. This allows you to define a function that extracts a comparison key from each element.
For instance, let’s say you have a list of tuples where each tuple contains a name and an age. You can sort by age easily:
In this example, the key argument uses a lambda function to extract the second item (age) from each tuple to sort the list accordingly.
sorted()A noteworthy aspect of sorted() is that it is a stable sort. This means that when multiple records have the same key, their original order is preserved.
For example, if you sort a list of objects by one attribute, and then by another, the first sort will not disrupt the order of items that were equal in the first sort.
Both 'apple' and 'banana' have the same count, but their order remains the same after sorting.
While basic sorting is useful, there are many scenarios where you might need more advanced techniques. Let’s discuss a few of those.
When sorting strings, Python uses lexicographic order, which means it compares the strings based on their Unicode values. This can lead to unexpected results if you're not careful.
Notice how 'Apple' comes before 'banana' because uppercase letters have lower Unicode values than lowercase letters. To sort in a case-insensitive manner, you can convert all strings to the same case:
You can sort by multiple criteria by passing a tuple to the key parameter. For example, if you have a list of people and you want to sort by age and then by name:
In this case, we first sort by age, and if two people share the same age, we sort by name.
reversed()The reversed() function provides a simple way to reverse the order of elements in an iterable. Unlike sorted(), which generates a new list, reversed() returns an iterator that can be converted to a list or looped through.
To use reversed(), just pass in your iterable:
This converts the reversed iterator into a list, giving you the reversed order of the original list.
reversed() with StringsYou can also use reversed() with strings, but keep in mind that it will return an iterator of characters. To convert it back to a string, you’ll need to join the characters back together:
This is a neat trick when you need to manipulate strings without extra libraries.
While both sorted() and reversed() are efficient for typical use cases, be mindful of performance when working with large datasets. The time complexity of sorted() is O(n log n), while reversed() operates in O(n).
When performance is critical, and if you only need to reverse a list, consider modifying the list in place with slicing:
This method is more efficient because it avoids creating a new list.
When working with sorted() and reversed(), it's essential to be aware of edge cases that can cause unexpected behavior.
Both functions handle empty iterables gracefully. For instance:
There's no error here; it simply returns an empty list.
Attempting to sort a list with mixed data types can lead to a TypeError. For example:
To avoid this, ensure that you're only sorting homogeneous data types or provide a custom key that can handle mixed types.
None ValuesIf your iterable contains None values, Python will place them at the beginning or end of the sorted list, depending on the sort order:
This behavior could be surprising, so always check your data before sorting.