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.

How to Use Lambda With the Sort Method

Give Feedback

In this lesson, you’ll see how to use a lambda for the key of the sort() method of lists. sort() has two parameters. key is None by default. reverse is False by default.

You won’t be altering reverse, but you will be looking at key because you can use a lambda expression to alter the behavior of sort(). When you use lambda, you can extend key to be much more versatile.

00:00 Lambda functions with .sort(), filter(), map(), and reduce(). In this section, you’re going to see lambda functions being used in conjunction with methods and functions. Firstly, you’ll see it being used for the key of the .sort() method of list, then to create the filter to filter iterables with filter(), a function being applied to all the elements of an iterable using map(), and the function being applied cumulatively to all the elements of an iterable using reduce().

00:34 Firstly, the .sort() method of list. As you can see from the definition here, .sort() has two parameters: key, which is None by default, and reverse, which is False by default.

00:48 We will not be altering reverse, but we will be looking at key as we can use a lambda expression to alter the behavior of .sort().

00:59 The .sort() method sorts the list in place using only less than (<) or greater than (>) comparisons between items.

01:07 Lambda functions allow us to extend key to become much more versatile. Here’s a quick example below. We have a list of names. We use a lambda expression to split the names and then sort by the surname, which is the last element of the list.

01:26 Now let’s see that code in action. Here you can see a list of names—in this case, Command Module Pilots in the Apollo program. And if we sort it, it will be sorted as you’d expect, in alphabetical order starting with the first letter. So let’s see that in action.

01:50 And you can see, we start out with 'Michael Collins' being first in the list. And after it’s sorted, 'Alfred Worden' is first in the list, 'Stuart Roosa' is last in the list.

02:02 What if we wanted to sort it by surname? Well, what we need to do is to use a lambda expression to split each entry up into separate elements, and then pass this to .sort().

02:15 That’s fairly easy to do with a lambda expression. So again, we pass key a lambda expression. Again I’ll choose xit could be any letter. And we’re going to going to go x.split() and that will split each entry up into separate words.

02:34 And then we want to send it the last one, which is indexed with -1. Now we’re passing the surname of each of these astronauts to .sort(), and it will sort them by that. Let’s see that in action.

02:51 And now you can see they’re sorted by order—'Collins', 'Evans', and so on, all the way up to 'Worden'.

03:03 So by default, this list of people will be sorted by the first entry alphabetically. It’d be sorted by name, so it would be this character which is taken into account first, et cetera.

03:17 Now, just as an aside, I’ve done this as a list of tuples with curved brackets purely so you can see this more easily—how they are differentiated. If you have a list of lists, it would work exactly the same way, but it’s a little harder to make it out onscreen, and it’s important that you can see what’s going on and not be confused about what you’re seeing.

03:39 If you apply the .sort() method,

03:46 that will sort them by their first name, in this case. We’re going to print out the list before, and then print out the list afterwards. And let’s see that run. So here you can see that .sort() has sorted this original list in alphabetical order of that first entry.

04:14 So, here’s our sorted version, and you can see it’s 'Gerald', then 'Jo', 'Karen', and 'Steve'—so just alphabetical order. But that is not what we want to do, so now going back to the code.

04:26 .sort() takes the argument key, and here we can put in the lambda expression, lambda x in this case, and then you’re going to set x to be the second element of each item on the list.

04:44 So this would be here, because the second element is there. Of course, it’s zero-indexed. So running this code now gives the result we’re looking for. You can see it’s sorted by age—28, 35, 58, and 72.

05:11 Lambda expressions can be used to obtain properties of objects for sorting as well. We’re going to have a small example here. Firstly, a class called Person will be set up, which takes a name and an age.

05:36 And the dunder method .__repr__() is also going to be defined to allow us to see these objects more clearly.

05:54 Now we can create some instances of the class

06:07 and then put them in a list.

06:15 We can print that list and then try to sort it. And once we’ve tried to sort it, we can print it out again. Let’s see that running. Ah. You can see we have a problem. And once you think about it, it makes sense.

06:39 There’s no way for .sort() to know what it should sort on, as these objects aren’t something it understands. We need to allow it to do that by using a lambda function.

06:51 Here we’ll put in the parameter key, pass it a lambda function, which in this case is going to be the .age of each of our people.

07:04 If we run it now,

07:09 we can see they’re now sorted by .age. We could just as easily do it using their .name.

07:20 And running again, they’re sorted by .name, not .age.

Justin Richie on July 21, 2020

At 1:25 in this video, what is the [-1] doing here?

Darren Jones RP Team on July 21, 2020

The [-1] gives the last element of a list (in this case the surnames as the names were split with .split(), so that the last element of that list would be the surname). You could use [1], but that would only work if each name given had only two elements - firstname, lastname. if anyone had a middle name, then it wouldn’t work correctly.

Become a Member to join the conversation.