Now that we've learned about lambdas, I want to revisit sorting. I want to touch upon a few of the more advanced things that you can do with the Python Sorting Routines. Let's take a look. In order to sort something, I need data to sort. So, I am again going to use range and random.shuffle to get a randomly shuffled list of 10 numbers, and then I'm going to sort them using data.sort. We know how the sort method works, so that should be as expected, but sort also takes a keyword argument called reverse. It defaults to false, but you can set reverse=True, and when you do that, it's sorts in reverse order. So, we would get the numbers in descending order instead of ascending order. Let's try it. So, you can see if I just call data.sort, the numbers are sorted in ascending order. If I call data.sort with reverse=True, they're sorted in descending order. Now, let's create a list of tuples so that we have something more interesting to sort. I'm going to use a list comprehension that iterates over all of the items in data, which is currently sorted in descending order, and then creates a tuple with each item, and then it randomly select the number using random.randrange with arguments of three and 15. So these numbers will be integers between three up to but not including 15. This will give me a list of tuples. Let's just confirm that. When it does, you can see here. And the first element of each tuple are the descending numbers 9, 8, 7, and so on. All right, so, we know that we can sort this list by calling datatups.sort. If it is a list, let's see what happens. It sorts it by the first element of the tuple. But what if that's not what I want, what if I really want to sort by the second element of the tuple? Let's see how we might do that. Well, conveniently, sort has another keyword argument called key, and this takes a function. So, if I pass a function as key in here, you can see, I'm using a lambda because a lambda is very convenient here. I just want to create a quick function that I'm going to use once to pass to the sort of method. My lambda takes one input, that's the pair, that's going to be each element of the list, and it evaluates the expression pairs[1]. So, what is this doing, and why do I want this? Well, if you pass a function as the key argument to sort before deciding what to do with an element, it calls this key function, and whatever comes back is what it uses to sort the list. So, this key function is effectively telling the sort method to sort the list based on the second element of each tuple rather than the first element of each tuple. And let's see if that works. As you can see now, the first tuple has a four as its second element. The next one has a five, then a seven, and so on. It does look like this changed the way that sort sorted the list of tuples. Now, let's do something even more interesting. Let's sort by the product. So, I will pass a lambda for the key function that takes the pair as input, and then multiplies pair[0] times pair[1]. It's saying, "Let's sort by the product." And I'm also going to set reverse to be true, so I have reverse=True here. Let's see what happens now. First element is 7 and 14, 98, then the next one is 90, and then 72, 6 and 10, 60. So, it does look like we sorted in descending order of the product of the elements of the tuple. Now, let's shuffle our list of tuples again, and then you sorted this time to sort them. Sorted also supports both the key and reverse keyword arguments. I'm just going to use a lambda here that evaluates to the second element of the tuple, and I'm going to set reverse=True, and see what happens. So, we can see that after calling sorted, the original tuples have not been modified from the shuffled tuples, and what is returned is sorted based upon the second element in descending order. The second element is 12, then 12, then 10, then 8, and so on. So, we can use either sort or sorted with these two keyword arguments. Key in reverse to control how we sort. In this video, I've shown you some of the more advanced features of Python Sorting, that allow you more control over how your sequences are actually sorted. The key argument allows you to pass a functioning, that changes what the sort routines use as a key to decide what order the elements should be in. And also, the reverse argument allows you to control which order things are sorted in, ascending or descending. So hopefully now, you have a better feel for the flexibility and power of the Python Sort Routines. Happy sorting!