Sorting lists in Python comes down to two questions. Do you want the original list reordered, and what exactly are you comparing? The first question is answered by sort() versus sorted(), and the second one by the key argument. Getting either of them wrong produces a bug that is very easy to misread.
The previous lesson in this module covered "Removing Items: pop, remove, del and clear", and the next lesson in this module is about "Copying Lists vs Referencing: The Default Trap".
What is the difference between sort() and sorted()?
list.sort() is a method. It rearranges the list in place and leaves you with the same object, in a new order. sorted() is a function. It takes any iterable and gives you back a new sorted list, leaving the original alone.
scores = [3, 1, 2]
scores.sort()
print('after sort:', scores)
original = [3, 1, 2]
ordered = sorted(original)
print('ordered:', ordered)
print('original:', original)
That difference matters more than it first appears. If two parts of your program hold references to the same list, then calling sort() changes what both of them see. If you sort a list that you received as an argument, then you have changed your caller's data as a side effect that is invisible in the signature of the function.
sorted() also accepts things that are not lists, such as a tuple, a set, a generator, or a dictionary, and in that last case it sorts the keys. It always returns a list.
print(sorted((3, 1, 2)))
print(sorted({'b', 'a', 'c'}))
print(sorted({'b': 2, 'a': 1}))
Sorting a set is worth a second thought. The set itself has no meaningful order, so sorted() is the only way to get a predictable sequence out of one, but the order that you get is a consequence of the values and not of any order that you chose. If you need the order in which you inserted them, then keep a list.
Why does sorting a list return None?
Because sort() changes the list and has nothing to hand back to you. This produces one of the most reliable beginner bugs in Python, because the mistake looks like it should work.
scores = [3, 1, 2]
scores = scores.sort()
print('scores is now:', scores)
try:
print(len(scores))
except TypeError as error:
print('TypeError:', error)
After the assignment, scores is None, because the right-hand side evaluated to None and that is what got assigned. The original list still exists somewhere, but you have thrown away the only name that pointed at it.
Two habits prevent this. Write scores.sort() as a statement and never assign its result. Or, if you were reaching for a new list anyway, use sorted(scores), which returns the list that you were expecting. When in doubt, prefer sorted, because the in-place version buys you a little memory and nothing else.
There is a related trap with the built-in methods that change a list in place. list.append, list.extend, list.sort, list.reverse, and list.insert all return None, because their job is the change and not the result. A chained call like items.append(1).sort() therefore fails on the very first link, with an error message that points at None instead of at the chain.
How do you sort by a key?
By default, Python compares the elements themselves, which works when they are all the same kind of thing. To sort by something else, pass key= a function. Python calls it once for each element and then compares the results instead of the elements.
words = ['Pee', 'wee', 'Bee']
print(sorted(words))
print(sorted(words, key=str.lower))
print(sorted(['bb', 'a', 'ccc'], key=len))
Three useful keys cover most cases. str.lower is for case-insensitive text, len is for length, and a lambda is for anything that you compute yourself. The lambda is the flexible one, and it is how you sort a list of dictionaries or objects by one of their attributes.
A special case of that is data that arrives as text but should be ordered as numbers. Sorting the strings directly puts '100' before '9', because the comparison happens character by character. A key of int converts each value first, and that gives you the order that a human expects.
numbers = ['10', '9', '100']
print(sorted(numbers))
print(sorted(numbers, key=int))
people = [
{'name': 'Ada', 'age': 36},
{'name': 'Grace', 'age': 45},
{'name': 'Alan', 'age': 41},
]
by_age = sorted(people, key=lambda person: person['age'])
print([person['name'] for person in by_age])
oldest_first = sorted(people, key=lambda person: person['age'], reverse=True)
print([person['name'] for person in oldest_first])
reverse=True flips the final order. It does not change how the key is worked out, so sorting by name descending still compares names, and it just puts Z before A. Notice that reverse=True is not the same as reversed(list). The first one sorts and then flips, and the second one only flips.
If your key is a single field, then operator.itemgetter is a leaner alternative to the lambda, and it reads well when the field name is the whole point of the sort.
from operator import itemgetter
staff = [{'name': 'Ada', 'age': 36}, {'name': 'Grace', 'age': 45}, {'name': 'Alan', 'age': 41}]
print(sorted(staff, key=itemgetter('age'))[0]['name'])
print(sorted([('b', 2), ('a', 2), ('c', 1)], key=itemgetter(1)))
The number inside itemgetter(1) means "the second element of each tuple", because indexing starts at zero. It is the same key function as lambda pair: pair[1], written without the overhead of a function call.
How do you sort by more than one field?
Return a tuple from the key function. Python compares tuples element by element, so the first element becomes the primary sort key and the rest act as tie-breakers.
staff = [
{'name': 'Ada', 'age': 36, 'city': 'London'},
{'name': 'Grace', 'age': 45, 'city': 'New York'},
{'name': 'Alan', 'age': 41, 'city': 'London'},
]
ordered = sorted(staff, key=lambda person: (person['city'], person['age']))
print([(person['city'], person['name']) for person in ordered])
The example sorts by city first and by age inside each city. Swap the two elements of the tuple and the priority swaps with them. If you want a descending sort on the second field only, then that is not something the tuple trick does directly, and you should sort by the first key, reverse the result, and then sort stably by the second one. Python's sort is stable, so equal keys keep their existing relative order, and that makes the two-step technique reliable.
What happens when you sort mixed types?
You get a TypeError. Python cannot decide whether 3 comes before 'two', so it refuses rather than guessing.
mixed = [1, 'two', 3.0]
try:
sorted(mixed)
except TypeError as error:
print('TypeError:', error)
Sorting a dictionary is not a special case. You sort its keys. When you want the entries sorted by value, then sort the items and give a key that reaches the second element of each pair.
scores = {'Ada': 36, 'Grace': 45, 'Alan': 41}
print(sorted(scores))
print(sorted(scores.items()))
print(sorted(scores.items(), key=lambda pair: pair[1]))
One more property is worth knowing, because it makes sorting by several keys predictable. Python's sort is stable. Elements whose keys compare equal keep the order that they had before the sort, so sorting by one key and then by another never scrambles the result of the first sort.
rows = [('a', 2), ('b', 1), ('c', 2), ('d', 1)]
print(sorted(rows, key=lambda row: row[1]))
Sorting cheat sheet
| Goal | Code | Original list |
|---|---|---|
| Sort in place, ascending | lst.sort() | reordered |
| New sorted list | sorted(lst) | untouched |
| Descending | sorted(lst, reverse=True) | untouched |
| Sort by a field | sorted(lst, key=lambda x: x["field"]) | untouched |
| Case-insensitive | sorted(lst, key=str.lower) | untouched |
| Two keys | sorted(lst, key=lambda x: (x["a"], x["b"])) | untouched |
Now try it yourself. Sort a list of ten names in a way that ignores case, and print both the sorted list and the untouched original. Then sort a list of dictionaries by two fields and reverse the primary one. Next, deliberately write x = x.sort() on a scratch list, catch the resulting TypeError, and replace it with the correct call. Finally, sort a dictionary's items by value and print the keys in that order.