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.
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.
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.
.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
Become a Member to join the conversation.