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.

The reverse Parameter

Give Feedback

00:00 Every example that we’ve looked at so far has resulted in the returned list being sorted in ascending order. What if you’re trying to make a scoreboard and need the largest numbers first or something in reverse alphabetical order?

00:13 While you could flip the resulting list around, sorted() contains a reverse parameter that you can set to True, and the resulting list will be reversed for you.

00:22 Let’s open up the interpreter and see how this works. Go ahead and make a new list called names, and set this equal to ['Harry', 'Suzy', 'Al', 'Mark'].

00:42 If you call sorted() on this,

00:45 then you’ll see that the names are returned in alphabetical order, so 'A', 'H', 'M', and 'S'. Now try this again, but this time when you call sorted() and pass in names, also say reverse and set this equal to True.

01:01 And now you can see that this is in reverse alphabetical order. That’s not too complicated, right? What you should note here is that even though the resulting list is in reverse alphabetical order, the sorting is still based on the first letter of each word.

01:19 And just for completeness, let’s go ahead and call sorted() again, do names, and say reverse=False.

01:29 You’ll get the default behavior that you’ve been seeing before, so you can tell that the default value for reverse is False. In the next video, you’re going to learn how to pass arguments to the key parameter, which is a little bit more complicated than reverse, but makes sorted() incredibly powerful for many different types of data.

Become a Member to join the conversation.