Python list sort lambda

How to Use Lambda With the Sort Method

How to Use Python Lambda Functions
Darren Jones 07:28

Mark as Completed
Supporting Material
Recommended Tutorial Course Slides [.pdf]
Give Feedback
  • Description
  • Transcript
  • Comments & Discussion [4]

In this lesson, youll 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 wont 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, youre going to see lambda functions being used in conjunction with methods and functions. Firstly, youll 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 [] comparisons between items.

01:07 Lambda functions allow us to extend key to become much more versatile. Heres 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 lets see that code in action. Here you can see a list of namesin this case, Command Module Pilots in the Apollo program. And if we sort it, it will be sorted as youd expect, in alphabetical order starting with the first letter. So lets see that in action.

01:50 And you can see, we start out with 'Michael Collins' being first in the list. And after its 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 Thats fairly easy to do with a lambda expression. So again, we pass key a lambda expression. Again Ill choose xit could be any letter. And were 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 were passing the surname of each of these astronauts to .sort[], and it will sort them by that. Lets see that in action.

02:51 And now you can see theyre 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. Itd 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, Ive done this as a list of tuples with curved brackets purely so you can see this more easilyhow they are differentiated. If you have a list of lists, it would work exactly the same way, but its a little harder to make it out onscreen, and its important that you can see whats going on and not be confused about what youre seeing.

03:39 If you apply the .sort[] method,

03:46 that will sort them by their first name, in this case. Were going to print out the list before, and then print out the list afterwards. And lets 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, heres our sorted version, and you can see its '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 youre 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, its zero-indexed. So running this code now gives the result were looking for. You can see its sorted by age28, 35, 58, and 72.

05:11 Lambda expressions can be used to obtain properties of objects for sorting as well. Were 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 weve tried to sort it, we can print it out again. Lets see that running. Ah. You can see we have a problem. And once you think about it, it makes sense.

06:39 Theres no way for .sort[] to know what it should sort on, as these objects arent something it understands. We need to allow it to do that by using a lambda function.

06:51 Here well 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 theyre now sorted by .age. We could just as easily do it using their .name.

07:20 And running again, theyre 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 wouldnt work correctly.

Zach Thomas on Sept. 26, 2021

When you have the key be lambda x, the function list.sort[key] automatically feeds each element in the list to the lambda function rather than the list itself?

Bartosz Zaczyński RP Team on Sept. 27, 2021

@Zach Thomas Not exactly. Both the sorted[] function and the .sort[] method accept an optional key parameter, which lets you define the order of your elements. By default, theyre sorted by value, which produces different results depending on their type:

>>> sorted[[42, 177, 13]] [13, 42, 177] >>> sorted[["42", "177", "13"]] ['13', '177', '42']

For example, numbers are sorted using total order, while strings are sorted in lexicographic order. You can override this default behavior by providing a custom callable, whether its a lambda expression or a fully-fledged function, to define your key. This callable will receive a single element from the list:

>>> fruits = ["watermelon", "fig", "plum", "apple", "orange"] >>> sorted[fruits, key=len] # Sort by string length ['fig', 'plum', 'apple', 'orange', 'watermelon']

Python uses the key to know how to compare two elements and decide which one should go first. That is especially helpful with custom data types, which might contain more than one attribute to sort by:

>>> from collections import namedtuple >>> Person = namedtuple["Person", "first_name last_name age"] >>> people = [ ... Person["Joe", "Doe", 42], ... Person["John", "Smith", 27], ... Person["Anna", "Smith", 31], ... ] >>> sorted[people, key=lambda x: x.age] [ Person[first_name='John', last_name='Smith', age=27], Person[first_name='Anna', last_name='Smith', age=31], Person[first_name='Joe', last_name='Doe', age=42] ]

You can use attrgetter to conveniently grab that attribute:

>>> from operator import attrgetter >>> sorted[people, key=attrgetter["age"]] [ Person[first_name='John', last_name='Smith', age=27], Person[first_name='Anna', last_name='Smith', age=31], Person[first_name='Joe', last_name='Doe', age=42] ]

Become a Member to join the conversation.

Video liên quan

Chủ Đề