Sorting list elements in Python: sort() and sorted()

TL, DR

Python lists are a great tool to handle data, and very often you will find useful to sort values in a list. There are two handy built-in methods to do so: sort() and sorted(). They sound similar, but they have quite different effects.

Order matters

Lists in Python are a great tool to organize data and iterate over them. You can use them for any type of data, whether it’s numbers, strings, or other lists.

Sometimes you will find useful to organize your list in a certain order. Maybe for iterating over it in a meaningful way, or for selecting values in a certain order.

There are two ways to do so:

  • A built-in list method called sort()
  • A built-in function called sorted()

They sound quite similar, but they do operate in a different way.

Sorting in place with sort()

The list built-in method sort() modifies the list in place and returns None. It is the most efficient way to order a list, but it will change the original object for good. So you better make sure you are not going to need anymore the original order.

This is an example of how sort() works:

>>> a = [5, 2, 3, 1, 4]
>>> a.sort()
>>> a
[1, 2, 3, 4, 5]

You can see that the list has been modified in place.

More flexibility with sorted()

If you think you may still need the original object in its order, you may be better off using the sorted() built-in function. This will return a new object without modifying the original one. for instance:

>>> a = [5, 2, 3, 1, 4]
>>> b = sorted(a)
>>> a
[5, 2, 3, 1, 4]
>>> b
[1, 2, 3, 4, 5]

By the way, the sorted() function also works on other iterable objects like dictionaries.

Adding options to your sorting

There are a couple of useful options you can use while sorting. The first one is to determine whether the sorting has to be in an ascending or descending fashion. The second one is about how to compare elements in your list.

Ascending or descending?

By default sort() and sorted() will return the value in ascending order. If you want to change that, you need to specify the reverse parameter. For instance:

>>> a = [5, 2, 3, 1, 4]
>>> a.sort(reverse=True)
>>> a
[5, 4, 3, 2, 1]

Ordering attributes

Sometimes you need to tune or select the attributes you are sorting for. This is particularly true when you are sorting complex objects. In order to achieve this, you can use the key parameter.

This parameter accepts a function (or other callable) that takes a single argument and returns a value to use for sorting purposes.

For instance, for complex objects:

>>> book_tuples = [
...     ('Novel', 'Campus', 7),
...     ('Science', 'Offsite', 2),
...     ('History', 'Offsite', 4),
... ]
>>> sorted(book_tuples, key=lambda book: book[2])   # sort by number of copies
[('Science', 'Offsite', 2), ('History', 'Offsite', 4), ('Novel', 'Campus', 7)]

I hope this post helped you to clarify the difference between sort() and sorted(), and make sure you can sort elements in a Python list the right way!

Related links

Do you like our content? Check more of our posts in our blog!