When to Use sorted() vs .sort()
Now you know the differences between
.sort() and a couple gotchas when you’re trying to sort things in Python. You might be wondering when to use one or the other, and a good way to see this is with a practical example.
Imagine you’re working for a 5k race on a project to collect the data for each runner. Your task is to capture each runner’s bib number and the time it took them to complete the race. Go ahead and open up your interpreter, and off the bat, go
from collections import namedtuple. And then define a
Runner, which will be a
namedtuple. And you’ll say
'Runner', and then its fields will be the bib number and duration.
During the race, you’re going to have an empty list at the start called
runners. What you’ll do is you’ll append to this list a
Runner with a bib number
00:59 and a duration. And I need a closing parenthesis there.
01:05 So, these values aren’t going to really matter, I’m just going to make sure that they’re the same length, and your durations aren’t important either. Just go ahead and make sure that the actual values don’t have to be in any specific order or anything. And this is because in a 5k race, not everybody starts at the same time so the order that they cross the finish line and become appended to this list doesn’t necessarily mean that they finished faster than the people behind them or slower than the people in front. I’m just going to add a couple more here.
Let’s say this person was like that. Okay! So you could go ahead and append as many here as you want, and let’s say the race is done and you want to take a look at
runners and you have your list here of all these
namedtuple objects. So, one thing you saw is that you know that the top five runners with the lowest times will win prizes.
So to work a little bit ahead, you go ahead and you say
runners and you can call
.sort() off that. And in here, you can pass your
You’re going to use a
And for here, for each
x you’re going to get an attribute,
you’re going to pass in
x, and you’re going to look for that duration.
Cool. And now you know that your top five runners are just the first five objects in the
runners list. Cool! And you can compare this—you can see that the durations increase now from
1400, and so on. So, you’re all done! You used
.sort(), and everybody got their prizes. Unfortunately, you still have something to do.
02:51 The race director comes over and mentions that because Python 3.8 was just released, every 38th person to cross the finish line will also get a prize. This is now a problem because there’s no way to go back to that original list that contained the order that the runners crossed the finish line.
So if you take a look at
runners now, you can see that everything is now sorted by this duration, even though when you appended everything to the list, it wasn’t in that order.
.sort() will change the list that it’s called on. In this example, using
sorted() to sort by duration probably would have been a better choice. If you’re working with important data and may need to return to the original data at some point, you’re better off using
sorted() and assigning the ordered lists to new variables. Otherwise, if you’re working with a copy of the data and you won’t need to go back to it,
.sort() is perfectly okay.
sorted() will also work on a number of different iterables, so if you’re not working with lists, you kind of have to use
The last thing to note for
.sort() is that because it works in place, it can be a lot harder to take up a lot of memory when you’re dealing with particularly long lists. And that’s it!
Hopefully, now you have a pretty good understanding of how
.sort() work in Python. Let’s take a few minutes to wrap up real quick and cover everything that you learned.
Become a Member to join the conversation.