Locked learning resources

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

Unlock This Lesson

Locked learning resources

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

Unlock This Lesson

Use a Custom Function for Sorting

00:00 There’s more. You can also use user-defined functions for sorting. So you can define a function—for example, get_second_element()that takes in an item and then returns the second element of that item, so item[1].

00:25 So now I’ve defined a little function that’s called get_second_element() that takes in one argument, item. So that needs to be a collection, and then it returns the element at position one of that collection.

00:37 Now I can use this get_second_element() function also as an argument to key. Let’s use a list of tuples.

00:45 I’ve defined a list called items that contains three tuples. First one has two integer elements, -10 and 2. The second one also two integer elements, 0 and 3.

00:56 And the third one also two integer elements, 10 and 1. So if you were to sort this items list just without

01:05 passing anything, then it’ll sort it the way that it’s currently oriented by the first element in each of the tuples. So -10 is the lowest value. That’ll be first.

01:16 Then 0 is the second lowest value, so that’ll be second, and 10 is the highest, so that will be third.

01:23 Now you want to sort it by the second integer in each of those tuples. So that will be from the first tuple 2, from the second tuple 3, and from the third tuple 1, which means that the third tuple should be first, the currently first tuple should be second, and the currently tuple in the middle should be the last one.

01:43 And you can do that by saying items.sort() and then pass to key your custom function. That gets the second element of an item. So I can say get_second_element.

01:56 Again, I’m not calling it so I’m not opening and closing the parentheses, but I’m passing the function object. And when I press Enter and then look at items again, you can see that now the first tuple in the list has the second element, 1.

02:10 The second is the second element, 2. And the third one has the second element, 3. So I just took these values to sort it, and that’s the type of flexibility that you get when you use user-defined custom functions to sort your lists.

02:27 And one last thing to note is that this is a list method. So this doesn’t work on tuples. Of course it doesn’t, because tuples are immutable. What you’re doing here is operating on the list element itself and changing it.

02:40 So if you try it with a tuple, then it wouldn’t work.

02:45 (1, 10, 5). And I attempted to sort it that way.

02:54 Then you get an AttributeError that the tuple object has no such attribute sort. So this is unique to lists because lists are mutable, so you can change them, and you can’t change the tuple once you’ve created it.

03:07 All right. That finishes the lesson on sorting lists and also actually finishes everything except, of course, that I’ve got another set of training exercises for you coming up in the next lesson.

03:20 And then your next step is the summary. So you’re done with the course. Let’s do a quick recap and then off on your way to work with all these great collection objects that you’ve just learned about.

Become a Member to join the conversation.