Join us and get access to hundreds of tutorials and a community of expert Pythonistas.

Unlock This Lesson

This lesson is for members only. Join us and get access to hundreds of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Hint: You can adjust the default video playback speed in your account settings.
Hint: You can set the default subtitles language in your account settings.
Sorry! Looks like there’s an issue with video playback 🙁 This might be due to a temporary outage or because of a configuration issue with your browser. Please see our video player troubleshooting guide to resolve the issue.

When to Use sorted() vs .sort()

Give Feedback

00:00 Now you know the differences between sorted() and .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.

00:14 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.

00:45 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.

01:39 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.

02:01 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 key.

02:09 You’re going to use a lambda function.

02:13 And for here, for each x you’re going to get an attribute,

02:19 you’re going to pass in x, and you’re going to look for that duration.

02:23 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 1050, 1350, 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.

03:07 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.

03:19 That’s because .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 sorted().

03:51 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!

04:02 Hopefully, now you have a pretty good understanding of how sorted() and .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.