1 00:00:07,970 --> 00:00:11,940 Welcome back. We're going to learn something that's extremely 2 00:00:11,940 --> 00:00:15,630 useful and powerful but conceptually a bit tricky. 3 00:00:15,630 --> 00:00:17,280 So, hang in there on this one. 4 00:00:17,280 --> 00:00:19,755 Once you get it, you're going to think, wow! 5 00:00:19,755 --> 00:00:23,410 This is really cool. At least I do. 6 00:00:23,510 --> 00:00:29,250 So, let's say we have a list of numbers like L1 created on line one, 7 00:00:29,250 --> 00:00:34,720 and we want to sort them on some property like their absolute value. 8 00:00:36,470 --> 00:00:39,120 So, from line nine, 9 00:00:39,120 --> 00:00:42,820 we're printing out the absolute value of three, 10 00:00:42,820 --> 00:00:45,045 which is just three. 11 00:00:45,045 --> 00:00:51,420 That the absolute value of minus 119 is 119. 12 00:00:51,420 --> 00:00:53,220 On lines 12 and 13, 13 00:00:53,220 --> 00:00:55,880 we're just going through each of the items in 14 00:00:55,880 --> 00:00:58,880 the list and we're outputting their absolute values. 15 00:00:58,880 --> 00:01:01,915 So, we get one, seven, four. 16 00:01:01,915 --> 00:01:03,380 Instead of minus two, 17 00:01:03,380 --> 00:01:07,560 we get its absolute value which is two and finally three. 18 00:01:07,560 --> 00:01:15,020 Now, suppose we want to sort L1 based on the absolute value, 19 00:01:15,020 --> 00:01:17,720 we can just tell the sorted function to use 20 00:01:17,720 --> 00:01:21,650 absolute value as the property that we want to sort by. 21 00:01:21,650 --> 00:01:28,650 The way we do that is we use this other optional parameter called key. 22 00:01:29,570 --> 00:01:35,090 Here's another optional parameter just like we had reverse in the previous video, 23 00:01:35,090 --> 00:01:38,230 here we've got key and we can specify a value, 24 00:01:38,230 --> 00:01:41,240 we're going to specify absolute and that 25 00:01:41,240 --> 00:01:45,760 tells the sorted function to sort by the absolute value. 26 00:01:50,520 --> 00:01:55,744 Here you can see that instead of having minus two first, 27 00:01:55,744 --> 00:02:01,300 minus two is coming after one because its absolute value is bigger. 28 00:02:01,300 --> 00:02:05,665 We've printed out the sorted version of L2. 29 00:02:05,665 --> 00:02:09,730 We can also do it in reverse order by combining the use of reverse 30 00:02:09,730 --> 00:02:14,340 equals true with key equals absolute, and we've got that here. 31 00:02:14,340 --> 00:02:17,095 Now, that one seems pretty straight forward 32 00:02:17,095 --> 00:02:20,450 until you start to really think about what's going on. 33 00:02:20,450 --> 00:02:25,360 The thing that we are passing as 34 00:02:25,360 --> 00:02:31,044 a value for the key parameter is the value of the variable absolute. 35 00:02:31,044 --> 00:02:34,075 That value is a function. 36 00:02:34,075 --> 00:02:42,190 So, we're passing a function absolute to another function sorted. 37 00:02:42,820 --> 00:02:48,320 I hope your first reaction to that is like it should blow your mind like huh! 38 00:02:48,320 --> 00:02:51,065 We're passing a function to a function. 39 00:02:51,065 --> 00:02:56,300 So, yeah that's really weird but eventually you're going to say, 40 00:02:56,300 --> 00:02:58,775 "Wow! That's really powerful." 41 00:02:58,775 --> 00:03:01,085 So, to make sense of this, 42 00:03:01,085 --> 00:03:02,750 we need to have a little mental model, 43 00:03:02,750 --> 00:03:06,265 a way to think about what's going on inside the sorted function. 44 00:03:06,265 --> 00:03:10,415 We're passing it in this function and what is it doing with that function? 45 00:03:10,415 --> 00:03:15,860 What it's doing is before it starts comparing any of the items to each other, 46 00:03:15,860 --> 00:03:18,400 remember comparisons that's like the pairs of 47 00:03:18,400 --> 00:03:21,085 dancers comparing their numbers to each other. 48 00:03:21,085 --> 00:03:23,920 So, before it does any of those comparisons, 49 00:03:23,920 --> 00:03:27,055 the sorted function uses the function that you pass in 50 00:03:27,055 --> 00:03:33,580 absolute and it uses that to determine numbers to assign to each of those dancers. 51 00:03:33,580 --> 00:03:35,990 That is behind the scenes, 52 00:03:35,990 --> 00:03:38,300 when you call sorted, 53 00:03:38,300 --> 00:03:42,790 sorted is going to call the function that you 54 00:03:42,790 --> 00:03:47,585 provide and it's going to call it once for each of the items in the sequence. 55 00:03:47,585 --> 00:03:53,290 It's going to do that to determine some property of that item like its absolute value, 56 00:03:53,290 --> 00:03:58,195 it's going to write it down on a little post-it note that the item carries around. 57 00:03:58,195 --> 00:04:02,420 Then, the sorted function does all the comparisons between the items 58 00:04:02,420 --> 00:04:07,385 but the comparison is always between the values that are on those post-it notes. 59 00:04:07,385 --> 00:04:11,240 So, for example, how is this working? 60 00:04:11,240 --> 00:04:14,255 We've got this sequence one, 61 00:04:14,255 --> 00:04:20,980 seven, four minus two, and three. 62 00:04:26,660 --> 00:04:32,435 Before we do any of those comparisons between pairs of items, 63 00:04:32,435 --> 00:04:37,970 we're going to run our function absolute on each of the items in 64 00:04:37,970 --> 00:04:43,085 turn and we're going to annotate the item with a little post-it note. 65 00:04:43,085 --> 00:04:48,060 So, we're going to have a post-it note for one that it's absolute value is one, 66 00:04:48,060 --> 00:04:51,590 and for seven that it's absolute value is seven, 67 00:04:51,590 --> 00:04:55,340 for four that it's absolute value is four. 68 00:04:55,340 --> 00:04:58,080 Those are uninteresting but four minus two, 69 00:04:58,080 --> 00:05:01,010 we get that the absolute value is two, 70 00:05:01,010 --> 00:05:03,305 and that's going to change things a bit. 71 00:05:03,305 --> 00:05:07,100 Then we go and do these pairwise comparisons one against seven 72 00:05:07,100 --> 00:05:10,715 and seven against four and seven against minus two and all of that. 73 00:05:10,715 --> 00:05:13,280 There's a whole bunch of those that happened behind the scenes in 74 00:05:13,280 --> 00:05:16,745 the sorted function but whenever it's doing a comparison, 75 00:05:16,745 --> 00:05:21,975 it uses the post-it notes to determine the order. 76 00:05:21,975 --> 00:05:32,520 So, we end up with one and then minus two and then three, four, and seven. 77 00:05:32,520 --> 00:05:37,105 The reason we end up with minus two coming after 78 00:05:37,105 --> 00:05:43,580 one is because we had the comparisons based on their post-it notes. 79 00:05:43,580 --> 00:05:46,950 One had the one but minus two had two. 80 00:05:46,950 --> 00:05:53,010 So, minus two with its post-it note of two ends up coming after one. 81 00:05:53,010 --> 00:05:57,260 So, if you think of that as being the process that's going on, 82 00:05:57,260 --> 00:05:59,610 you invoke the sorted function, 83 00:05:59,610 --> 00:06:02,865 the sorted function calls your function, 84 00:06:02,865 --> 00:06:08,600 like absolute, and it calls it once on each of the items in the sequence. 85 00:06:08,600 --> 00:06:12,950 I'm going to prove to you that that really is what's going on. 86 00:06:12,950 --> 00:06:15,695 The way I'm going to prove that is, 87 00:06:15,695 --> 00:06:20,300 I've modified the absolute function here, 88 00:06:20,300 --> 00:06:21,890 just a little bit, 89 00:06:21,890 --> 00:06:24,925 by having it prints something out. 90 00:06:24,925 --> 00:06:28,005 Just added this thing on line four. 91 00:06:28,005 --> 00:06:31,475 So, every time the absolute function gets invoked, 92 00:06:31,475 --> 00:06:33,900 we're going to print something out. 93 00:06:34,420 --> 00:06:38,830 So, on line 10, we're saying to print something, 94 00:06:38,830 --> 00:06:42,080 then we call the sorted function and then we print something to say, 95 00:06:42,080 --> 00:06:44,100 "Hey, we're done with sorting." 96 00:06:49,300 --> 00:06:59,885 Well, you see that line 10 generates that and line 12's print statement 97 00:06:59,885 --> 00:07:05,420 saying that we're done is there then everything in between is stuff that's 98 00:07:05,420 --> 00:07:11,495 happening because of calls to absolute that happened during the execution of line 11. 99 00:07:11,495 --> 00:07:17,265 So, we pass in the value absolute to the sorted function. 100 00:07:17,265 --> 00:07:24,700 We never actually say invoke the absolute function. 101 00:07:25,270 --> 00:07:31,260 We never do that but inside the execution of the sorted function, 102 00:07:31,260 --> 00:07:33,600 it is calling absolute, 103 00:07:33,600 --> 00:07:36,450 and it's calling it one, 104 00:07:36,450 --> 00:07:40,300 two, three, four, five times, 105 00:07:40,300 --> 00:07:44,150 once for each of the five items in 106 00:07:44,150 --> 00:07:49,415 the list and you can see it's calling it for the value one for the value seven, 107 00:07:49,415 --> 00:07:53,230 four, minus two, and three. 108 00:07:53,230 --> 00:07:58,424 So, that's what's going on behind the scenes when the sorted function is executing, 109 00:07:58,424 --> 00:08:03,020 just so we can get the right numbers onto the post-it notes so that it can 110 00:08:03,020 --> 00:08:08,550 use those for doing all the comparisons that lead to sorting of the items. 111 00:08:10,010 --> 00:08:14,680 So, the thing that you pass in for the key parameter has to be a function. 112 00:08:14,680 --> 00:08:16,930 So, often we'll call it the key function. 113 00:08:16,930 --> 00:08:18,880 It has to be a function, 114 00:08:18,880 --> 00:08:22,400 like absolute is here, it's a function. 115 00:08:22,400 --> 00:08:26,140 It has to be a function that takes one input, 116 00:08:26,140 --> 00:08:31,525 that input is going to be one of the items at a time from the list, 117 00:08:31,525 --> 00:08:36,165 and it has to return some value that's going to go on the post-it note. 118 00:08:36,165 --> 00:08:40,175 Some property of the item or typically a number. 119 00:08:40,175 --> 00:08:46,990 Now, I just want to mention one thing here about passing in a function. 120 00:08:46,990 --> 00:08:49,780 We can either pass a function by name, 121 00:08:49,780 --> 00:08:52,170 as we did in originally with this, 122 00:08:52,170 --> 00:08:58,295 we just say the absolute function or we can pass in a lambda expression. 123 00:08:58,295 --> 00:09:01,010 Remember from a previous lesson that 124 00:09:01,010 --> 00:09:05,300 lambda expressions are expressions that produce anonymous functions. 125 00:09:05,300 --> 00:09:08,810 So, instead of specifying absolute here, 126 00:09:08,810 --> 00:09:12,570 we could specify a lambda expression. 127 00:09:22,740 --> 00:09:26,890 So, this is a lambda expression. 128 00:09:27,690 --> 00:09:33,585 This whole expression evaluates to a function object. 129 00:09:33,585 --> 00:09:37,220 That function takes one input x and returns as 130 00:09:37,220 --> 00:09:42,030 its value whatever this expression returns. 131 00:09:42,520 --> 00:09:46,945 Now, if you write this like this, 132 00:09:46,945 --> 00:09:51,935 it might look a little silly to inexperienced programmer because really 133 00:09:51,935 --> 00:09:55,640 this lambda expression is returning a function that does 134 00:09:55,640 --> 00:10:00,175 exactly the same thing that absolute does. 135 00:10:00,175 --> 00:10:07,400 But, you may still want to do that just if it makes it clearer for you. 136 00:10:07,400 --> 00:10:09,620 This is a conceptually challenging thing, 137 00:10:09,620 --> 00:10:13,270 this idea of passing in a function for the key parameter. 138 00:10:13,270 --> 00:10:17,240 I find a lot of students really they just understand it better this 139 00:10:17,240 --> 00:10:21,535 way where the lambda reminds them that they are producing a function. 140 00:10:21,535 --> 00:10:26,830 When they just see key equals absolute, 141 00:10:26,830 --> 00:10:32,375 it doesn't quite click that absolute is referring to a function object. 142 00:10:32,375 --> 00:10:34,145 So, if this helps you, 143 00:10:34,145 --> 00:10:35,900 you're welcome to do it this way. 144 00:10:35,900 --> 00:10:39,815 There are other places where we'll have other lambda expressions 145 00:10:39,815 --> 00:10:43,975 which make even more sense and we'll see that in a later lesson. 146 00:10:43,975 --> 00:10:49,370 So, to summarize, If you want to sort a sequence based on some property of the items, 147 00:10:49,370 --> 00:10:53,825 call sorted and pass a value for the key parameter. 148 00:10:53,825 --> 00:10:56,765 The value for the key parameter has to be a function, 149 00:10:56,765 --> 00:11:02,030 that function takes one item as input and returns a value to write on the post-it note, 150 00:11:02,030 --> 00:11:04,340 a property of the item. 151 00:11:04,340 --> 00:11:09,530 The value for the key parameter can either be the name of a function, 152 00:11:09,530 --> 00:11:12,625 like absolute, or a lambda expression, 153 00:11:12,625 --> 00:11:15,680 like I've shown on line nine here. 154 00:11:15,680 --> 00:11:19,445 So, play around with this a little, do some exercises, 155 00:11:19,445 --> 00:11:22,820 keep trying it until it makes sense and then you'll be able to 156 00:11:22,820 --> 00:11:27,420 sort anything anytime. See you next time.