Sorting Lists

Python provides us with a few tools for sorting and reordering the elements of a list. In this section, we will discuss the following functions: the sorted() function, the sort() method, and the reverse() method.

sorted() Function

We can use sorted() to obtain a sorted version of the list. Note that this does not alter the order of the original list. It creates list which contains the same elements of the original list, but in sorted order. We can then store this new list in a variable, print it, or provide it to another function.

listA = [6, 9, 1, 4, 7, 3, 6, 4, 5, 2]
listA_asc = sorted(listA)

print('Sorted List:  ', listA_asc)
print('Original List:', listA)
Sorted List:   [1, 2, 3, 4, 4, 5, 6, 6, 7, 9]
Original List: [6, 9, 1, 4, 7, 3, 6, 4, 5, 2]

The sorted() function sorts items in ascending order by default. We can change this behavior by setting an optional parameter named reverse. This parameter accepts a Boolean value, and is set equal to False by default. If reverse is set to True, then the list will be sorted in descending order.

listA_desc = sorted(listA, reverse=True)

print('Sorted List:  ', listA_desc)
print('Original List:', listA)
Sorted List:   [9, 7, 6, 6, 5, 4, 4, 3, 2, 1]
Original List: [6, 9, 1, 4, 7, 3, 6, 4, 5, 2]

The sort() Method

As mentioned above, the sorted() function does not alter the order of the original list, but instead creates a new list. There are occasions when we only intend to work with a sorted version of the list, and would thus perfer to perform the sort on the original list itself, rather than creating a new list. One option would be to use sorted(), and then store the results back into the variable that held the original list. However, a simpler approach would be to use the sort() list method.

The sort() method for lists will sort the elements of a list in ascending order. The method does not create any new lists, but instead rearranges the elements of the orignal list. This is referred to as “in place” sorting.

listB = [6, 9, 1, 4, 7, 3, 6, 4, 5, 2]
listB.sort()
print(listB)
[1, 2, 3, 4, 4, 5, 6, 6, 7, 9]

Like the sorted() function, the sort() method has an optional reverse parameter that can be used to sort the list in descending order.

listC = [6, 9, 1, 4, 7, 3, 6, 4, 5, 2]
listC.sort(reverse=True)
print(listC)
[9, 7, 6, 6, 5, 4, 4, 3, 2, 1]

sorted() vs sort()

The fact that sort() performs an in-place sort while sorted() creates a new list illustrates a difference between methods and lists. Methods can have permission to directly alter the value of the object that they are working on, while other types of function generally do not.

You might be asking how to decide which of these sorting functions to use if you need to sort a list. The answer essentially boils down to whether or not you need to preserve the original list, or if you only intend to ever work with the sorted version of the list. If you need need to preserve the original order, then you should sort using sorted(). However, if don’t need the original ordering and will only work with the sorted version of the list, then you should use sort().

The reverse() Method

Lists also have a reverse() method when reverses the order of a list. This re-ordering is performed on the elements of the original list, and does not create any new lists. That is, it is done “in place”.

listD = [6, 9, 1, 4, 7, 3, 6, 4, 5, 2]
listD.reverse()
print(listD)
[2, 5, 4, 6, 3, 7, 4, 1, 9, 6]

Note that reverse() does not sort the list in descending order. It simply reverses the current order of the list. We saw previously that we can obtain an in-place sort of a list in descending order by specifying reverse=True when using the sort() method. We could also call the sort() method on a list to first sort the list in ascending order, and then call the reverse() method on the list to put the list into descending order. However, this would require two lines of code. We illustrate this technique in the cell below.

listE = [6, 9, 1, 4, 7, 3, 6, 4, 5, 2]
listE.sort()
listE.reverse()
print(listE)
[9, 7, 6, 6, 5, 4, 4, 3, 2, 1]