A powerful built-in Python function that is great for working with sequences of data is the function sorted(). This function takes in a sequence as the mandatory input and can also take in an optional second keyword argument named reverse. The ‘reverse’ argument can be set to either ‘True’ or ‘False’. If it is not specified in the function call, ‘reverse’ is set to False by default meaning ascending order is used during the sort operation. If descending order what you are looking for, then set reverse to True. The sorted() function returns a new sorted list from the items in the given sequence. It does not modify the given sequence itself, and this is a key point to understand since there are other ways to sort in Python which sort data in place. We can look at several examples of the Python sorted() function in this tutorial.
Sort Strings Ascending Alphabetical
The first sorting example we can look at is a list of strings. So let’s populate a list with the names of some famous guitar players.
Now let’s call the sorted() function and pass in the ‘guitarists’ variable. We store the result of that function call in a new variable named ‘guitarists_sorted_a’. The new variable now holds the ascending alphabetical version of the original ‘guitarists’ variable.
A key point with the sorted() function is that it does not modify the original iterable. To get access to the sorted version of the iterable, you can store the return value of calling sorted() in a new variable as we saw above.
Sort Strings Descending Alphabetical
The sorted() function has an optional second parameter named ‘reverse’ that you can pass to it. We can use the ‘reverse’ keyword with a value of True to sort our list of guitarists in descending alphabetical order now.
Once again, the original list stays unmodified.
Sort Numbers In A Tuple
Now, let’s use the sorted() function with some numbers. We’ll start by adding a ‘numbers’ variable and storing a tuple of some random integer values.
Now we can call the sorted() function and pass in the ‘numbers’ variable. The result of this function call is stored in a new variable called ‘numbers_sorted_a’. The output shows that the new variable now has the integers sorted from lowest value to highest value.
Just like in our strings example, the original iterable is not modified. The ‘numbers’ variable is still the same as it was.
Our goal now is to sort the numbers tuple in descending order. We can use our handy ‘reverse’ argument set to True to accomplish this.
Sorting Floats In A List
For this example of the sorted() function, we have a list of floating-point numbers.
Now we can get a sorted version of this list by calling the sorted() function and passing in the ‘floats’ variable. The result is a list of floats sorted in ascending order.
We recall how to use ‘reverse’ with sorted(), so let’s use it on this list of floating-point values as well.
Using sorted() With A key
There is a third parameter that can be used with sorted(), and that is the ‘key’ parameter. It is a function that serves as a key for the sort comparison. In this next example, we start with a list of tuples. Each tuple has two values. When we used sorted() with this data structure, it sorts by the first value in the tuple.
What if we want to sort the list of tuples by the second value rather than the first? We can use the key function for that.
Python sorted() Function Summary
The Python sorted() function returns a sorted list from the items contained in an iterable. The resulting sort ascending by default, unless you provide the ‘reverse=True’ parameter in which case the sort is descending.
The syntax of the sorted() function is as follows:
sorted(iterable, key=None, reverse=False)