Sorting
In this lesson, you’ll learn how to sort iterables in Python. You can either use the sorted()
built-in function, or .sort()
method. You can pass in the optional key
parameter to specify how to sort the iterable, and you can reverse with the reverse
parameter:
>>> animals = [{'type': 'cat', 'name': 'Stephanie', 'age': 8}, {'type': 'dog', 'name': 'Devon', 'age': 3}, {'type': 'rhino', 'name': 'Moe', 'age': 5}]
>>> sorted(animals, key=lambda animal: animal['age], reverse=True)
[{'type': 'cat', 'name': 'Stephanie', 'age': 8}, {'type': 'rhino', 'name': 'Moe', 'age': 5}, {'type': 'dog', 'name': 'Devon', 'age': 3}]
If you want to learn more, check out Sorting Data With Python.
00:00
Let’s quickly talk about sorting in Python. This is really useful in interviews because many questions require you to sort in their solutions. Let’s start with the list. Let’s have a list of animals
.
00:11
We’ll have a "cat"
, we’ll have a "dog"
, we’ll have a "cheetah"
, and a "rhino"
. Let’s call the sorted()
function on animals
and see what that returns.
00:22
It actually returns a new list, so animals
is not changed at all, and it returns a sorted version of that list, so "cat"
comes alphabetically before "cheetah"
, and then "cheetah"
comes before "dog"
, et cetera. If we pass in the reverse=True
param, this will just reverse the order.
00:41
Let’s look at a little bit more complicated example. Let’s have a list of animals
that will be defined by a dictionary. We’ll have the 'type'
, which will be a 'cat'
. We’ll have the 'name'
, which will be 'Stephanie'
. We’ll have the 'age'
, which will be 8
. Let’s make a 'dog'
.
01:03
'dog'
, 'name': 'Devin'
the dog. 'age': 3
. One more—we’ll have a 'rhino'
, 'name': 'Mo'
the rhino…
01:23
like this. So, what happens if we call sorted()
on animals
? It actually errored, because you can’t compare dictionaries. It doesn’t know what to do. You can compare strings and you can compare numbers—so, sorting a list of numbers would sort it numerically—but you can’t compare dictionaries.
01:39
But they actually let us pass in a key
param that you can define your own comparing function. So, we have lambda
of animal
, and we will be using the animal’s age.
01:52
Perfect! So now, it’s sorted by 'age'
, and you could even pass in the reverse=True
. So, this might be a cool interview question. Given a list of animals
, get the oldest animal.
02:06
There. A one-line solution. There’s another way to sort, and that would be calling the .sort()
method, and that would actually mutate the list and sort it.
02:14
So if we call animals.sort()
with our key=lambda animal
, using the 'age'
,
02:23
that will actually change our list to now be sorted by 'age'
. In the next section, you will learn how to leverage data structures effectively in Python.
James Uejio RP Team on April 26, 2020
@Piotr that is a great point and yes it would be better to iterate over it and get the max value that way. I do that in Part 4.
James Uejio RP Team on April 27, 2020
If you want to learn more, here is a Real Python walkthrough video on sorting: Sorting Data With Python
elmorem on June 5, 2020
I appreciate this video and the clever little tip about the key parameter with dictionaries. Plus, no one would reasonably conclude that a little python magic like this means the underlying algorithm is somehow faster. also, as far as i’m following this, iterating over the list and then getting the max to produce the sort isn’t going to happen in O(n) time.
shangcode on Jan. 7, 2023
^_^ A single quotation is missing in Description tab. Animal[‘age’]
Become a Member to join the conversation.
Piotr on April 25, 2020
Isn’t it somewhat wrong to suggest to use sorting to get the oldest animal in your example?
I don’t reckon the interviewer would be impressed to see a O(n log n) solution instead of simple iteration over a list resulting in a O(n).