1 00:00:07,970 --> 00:00:12,810 Welcome back. One sorting task that comes up frequently, 2 00:00:12,810 --> 00:00:14,160 but it's a little tricky, 3 00:00:14,160 --> 00:00:16,155 is sorting a dictionary. 4 00:00:16,155 --> 00:00:18,360 The way we sort a dictionary, 5 00:00:18,360 --> 00:00:20,280 is to sort its keys. 6 00:00:20,280 --> 00:00:25,350 After that, we can iterate through the keys and look up values as we need to. 7 00:00:25,350 --> 00:00:28,150 So, remember this code? 8 00:00:30,080 --> 00:00:32,570 You've seen something like it before, 9 00:00:32,570 --> 00:00:35,280 maybe with a slightly different list. 10 00:00:36,740 --> 00:00:39,420 We've just got a list with a bunch of items. 11 00:00:39,420 --> 00:00:45,170 Some of them are repeat and we're creating a dictionary that counts for every item. 12 00:00:45,170 --> 00:00:48,710 For every distinct possible value in the list, 13 00:00:48,710 --> 00:00:51,005 how many times does it come up in the list? 14 00:00:51,005 --> 00:00:54,950 So, A comes up once, twice. 15 00:00:54,950 --> 00:00:58,180 So, at the end in our dictionary D, 16 00:00:58,180 --> 00:01:03,775 we're going to have the value two associated with the key A, 17 00:01:03,775 --> 00:01:06,945 and then on lines nine and 10, 18 00:01:06,945 --> 00:01:10,810 we're just printing out the values from that list. 19 00:01:13,490 --> 00:01:17,390 So, E appears twice, F appears once, 20 00:01:17,390 --> 00:01:19,610 A appears two times, 21 00:01:19,610 --> 00:01:22,220 as I just manually countered and so on. 22 00:01:22,220 --> 00:01:25,620 Notice that the keys E, 23 00:01:25,620 --> 00:01:27,760 F, A, B, C and so on, 24 00:01:27,760 --> 00:01:31,300 they appear not in any special order, 25 00:01:31,300 --> 00:01:35,150 and that's just the way dictionaries work. 26 00:01:35,150 --> 00:01:37,205 When you ask for the keys, 27 00:01:37,205 --> 00:01:42,515 you get all the keys back but there's no promise made about what order they'll appear in. 28 00:01:42,515 --> 00:01:45,545 Suppose I cared about the order, 29 00:01:45,545 --> 00:01:53,525 and I really wanted to say A appears one times, 30 00:01:53,525 --> 00:01:56,940 B appears twice and so on. 31 00:01:57,140 --> 00:02:01,500 The way to do that is going to be on line nine 32 00:02:01,500 --> 00:02:06,360 to sort the keys before doing the iteration. 33 00:02:06,360 --> 00:02:12,770 So, let's suppose that instead of just asking for the keys, 34 00:02:13,300 --> 00:02:18,180 I pass those keys into the sorted function. 35 00:02:21,130 --> 00:02:26,150 Now, I am going to get the results in alphabetic order. 36 00:02:26,150 --> 00:02:27,590 A appears two times, 37 00:02:27,590 --> 00:02:31,260 B appears two times, and so on. 38 00:02:31,990 --> 00:02:35,060 Well that's all well and good. 39 00:02:35,060 --> 00:02:40,880 How about if we wanted to sort based on the counts instead? 40 00:02:40,880 --> 00:02:45,905 So, we really want D to get printed out first because it appeared the most times, 41 00:02:45,905 --> 00:02:49,915 and after that we'll get the things that appeared only twice. 42 00:02:49,915 --> 00:03:00,060 Now, we can specify a property of these keys that we want to use for sorting. 43 00:03:00,220 --> 00:03:03,620 There's a little confusion that's going to go on here 44 00:03:03,620 --> 00:03:06,845 because we're using the word "key" in two different ways. 45 00:03:06,845 --> 00:03:10,235 We use the word "key" to refer to a key in a dictionary. 46 00:03:10,235 --> 00:03:13,370 Like in this dictionary D, 47 00:03:13,370 --> 00:03:18,940 we have the letter capital A is a key and capital B as a key and so on. 48 00:03:18,940 --> 00:03:23,030 Then we have a second use of the word "key" 49 00:03:23,030 --> 00:03:26,855 which is the parameter name in the sorted function. 50 00:03:26,855 --> 00:03:28,640 So, if we want to say, 51 00:03:28,640 --> 00:03:32,360 sort these keys based on some property of them, 52 00:03:32,360 --> 00:03:36,290 we say key equals and we pass in a function here. 53 00:03:36,290 --> 00:03:39,230 These are just two different meanings 54 00:03:39,230 --> 00:03:42,200 of the word" key" and you've got to keep them separated. 55 00:03:42,200 --> 00:03:47,335 So, remember the key function is going to take one list item as an input. 56 00:03:47,335 --> 00:03:50,090 In our case we have a list of all the keys A, 57 00:03:50,090 --> 00:03:51,260 B, C, D, E, 58 00:03:51,260 --> 00:03:56,450 F and so on, and so that's what we're going to have as one input, 59 00:03:56,450 --> 00:03:59,465 and I'm going to call my function, 60 00:03:59,465 --> 00:04:03,350 the parameter for this function K just to remind me that 61 00:04:03,350 --> 00:04:07,540 the thing that's getting passed into it is one key from the dictionary, 62 00:04:07,540 --> 00:04:09,570 like the letter A or the letter 63 00:04:09,570 --> 00:04:15,225 F. I'm choosing the parameter named K to remind me of that, 64 00:04:15,225 --> 00:04:20,180 and then we're going to return a property of that key. 65 00:04:20,180 --> 00:04:21,900 The property we want is, 66 00:04:21,900 --> 00:04:27,450 what is the value associated with that key in the dictionary D? 67 00:04:27,750 --> 00:04:33,349 So, if I have the key C, 68 00:04:33,349 --> 00:04:37,130 what I want to do is get the value one, 69 00:04:37,130 --> 00:04:39,625 and I want to use that for the sort order. 70 00:04:39,625 --> 00:04:43,085 For D, I want to use four. 71 00:04:43,085 --> 00:04:45,995 The way I can do that, 72 00:04:45,995 --> 00:04:52,165 is I refer to D square bracket K. Just look up for the current key, 73 00:04:52,165 --> 00:04:56,395 what is its value in the dictionary and I get, 74 00:04:56,395 --> 00:04:59,050 if the current key is C I'll get one, 75 00:04:59,050 --> 00:05:01,910 and if the current t is D I'll get four. 76 00:05:01,910 --> 00:05:04,730 This is really all I need to do in order to 77 00:05:04,730 --> 00:05:09,650 re-sort this output in the way that I want it. 78 00:05:10,930 --> 00:05:15,090 Let me just clear those markings for you. 79 00:05:18,190 --> 00:05:24,080 So, now we have D appearing at the end because we're going lowest to highest, 80 00:05:24,080 --> 00:05:29,780 the items that occur least frequently to the ones that occur most frequently. 81 00:05:29,780 --> 00:05:31,865 If I wanted to do it in the reverse order, 82 00:05:31,865 --> 00:05:41,990 I just use it reverse equals true, 83 00:05:41,990 --> 00:05:43,865 like we've done before. 84 00:05:43,865 --> 00:05:48,510 Now, D will get printed out first. 85 00:05:49,630 --> 00:05:52,915 So, the things to remember here are, 86 00:05:52,915 --> 00:05:56,705 we're not doing anything new with sorting. 87 00:05:56,705 --> 00:05:58,690 There's no new mechanics here. 88 00:05:58,690 --> 00:06:02,434 We're just passing in a function for the key parameter, 89 00:06:02,434 --> 00:06:04,610 but we have, perhaps, 90 00:06:04,610 --> 00:06:06,665 a little bit of a confusing function. 91 00:06:06,665 --> 00:06:12,230 This function is taking one key from the dictionary as its input, 92 00:06:12,230 --> 00:06:15,020 and returning a property of that key, 93 00:06:15,020 --> 00:06:19,640 the look up of its value in the dictionary D. So, 94 00:06:19,640 --> 00:06:22,580 we have key as in a key from the dictionary, 95 00:06:22,580 --> 00:06:32,500 that's our letter K. We've chosen to remind us that we're dealing with a dictionary key, 96 00:06:32,500 --> 00:06:39,480 and then we have the parameter name key for the sorted function. 97 00:06:41,720 --> 00:06:45,385 One other thing I want to point out, 98 00:06:45,385 --> 00:06:51,920 is that when we tell the sorted function to sort the keys, 99 00:06:51,920 --> 00:06:54,100 there's a shorthand we can use. 100 00:06:54,100 --> 00:06:59,395 You may recall we've said before that anytime a list is expected, 101 00:06:59,395 --> 00:07:03,140 there's some place in the code where the Python interpreter is expecting a list. 102 00:07:03,140 --> 00:07:05,060 If you provide a dictionary, 103 00:07:05,060 --> 00:07:08,960 it will automatically grab all of the keys as the list. 104 00:07:08,960 --> 00:07:10,370 So, this is equivalent. 105 00:07:10,370 --> 00:07:14,005 We can either say D.keys or we can just say D, 106 00:07:14,005 --> 00:07:15,600 because we're passing them to sorted. 107 00:07:15,600 --> 00:07:17,325 If we pass the dictionary, 108 00:07:17,325 --> 00:07:21,660 it automatically figures out that we want to sort the keys. 109 00:07:22,600 --> 00:07:28,550 So, this is sorting a dictionary's keys based on their values. 110 00:07:28,550 --> 00:07:30,730 There are several useful exercises at the bottom 111 00:07:30,730 --> 00:07:32,870 of the page in the textbook that I encourage you to work 112 00:07:32,870 --> 00:07:38,140 through in order to solidify your understanding. Will see you next time.