1 00:00:00,450 --> 00:00:02,070 - [Instructor] In this video, we're going to discuss 2 00:00:02,070 --> 00:00:05,110 the dictionary method's keys and values. 3 00:00:05,110 --> 00:00:07,680 And I'm also going to talk in a little bit more detail 4 00:00:07,680 --> 00:00:10,450 about the items method as well. 5 00:00:10,450 --> 00:00:12,250 Now for demonstration purposes, 6 00:00:12,250 --> 00:00:14,390 we'll use this months dictionary, 7 00:00:14,390 --> 00:00:17,410 which contains three key value pairs, once again, 8 00:00:17,410 --> 00:00:20,730 and let's start out within a for loop here 9 00:00:20,730 --> 00:00:23,710 in which we demonstrate the keys method. 10 00:00:23,710 --> 00:00:26,710 Now, the items method that we looked at previously 11 00:00:26,710 --> 00:00:30,550 gave us back each key value pair as a tuple, 12 00:00:30,550 --> 00:00:34,450 the keys method simply grabs each key 13 00:00:34,450 --> 00:00:36,630 from the dictionary and gives us back 14 00:00:36,630 --> 00:00:38,900 something that's iterable that I can then 15 00:00:38,900 --> 00:00:41,270 walk through and in this case I'll just 16 00:00:41,270 --> 00:00:44,320 display the month name followed by a couple of spaces. 17 00:00:44,320 --> 00:00:45,940 So let's go ahead and do that. 18 00:00:45,940 --> 00:00:49,460 Now, as you can see here, the keys came out 19 00:00:49,460 --> 00:00:52,140 in the same order that they were inserted 20 00:00:52,140 --> 00:00:54,280 into the dictionary once again, 21 00:00:54,280 --> 00:00:56,720 and that still is going to depend on 22 00:00:56,720 --> 00:00:58,630 what version of Python you're using. 23 00:00:58,630 --> 00:01:02,170 Now, if you installed Python three dot seven and higher, 24 00:01:02,170 --> 00:01:06,090 as I specified back in the before you begin lesson, 25 00:01:06,090 --> 00:01:08,940 then you'll wind up... you should wind up with 26 00:01:08,940 --> 00:01:10,930 the same order that I'm seeing here, 27 00:01:10,930 --> 00:01:13,990 however, if you're using an earlier version of Python, 28 00:01:13,990 --> 00:01:15,610 the order in which these come out 29 00:01:15,610 --> 00:01:18,720 may be different on your particular machine. 30 00:01:18,720 --> 00:01:22,020 Now, let's also go ahead and demonstrate the values method. 31 00:01:22,020 --> 00:01:24,800 As you would expect, it's going to give you back the values. 32 00:01:24,800 --> 00:01:27,730 Notice that the values for the corresponding keys 33 00:01:27,730 --> 00:01:30,700 came out in the same order as again, 34 00:01:30,700 --> 00:01:33,840 they were defined in the original dictionary. 35 00:01:33,840 --> 00:01:36,550 Now, I said when we first introduced 36 00:01:36,550 --> 00:01:39,330 the items method, that it was returning 37 00:01:39,330 --> 00:01:42,360 an iterator that allowed us to iterate through 38 00:01:42,360 --> 00:01:44,960 in our case, the items previously, 39 00:01:44,960 --> 00:01:47,210 and now, the keys and the values. 40 00:01:47,210 --> 00:01:49,670 But in all three of these methods' cases, 41 00:01:49,670 --> 00:01:52,000 what's really being returned is 42 00:01:52,000 --> 00:01:55,100 what's known as a dictionary view. 43 00:01:55,100 --> 00:01:57,830 A dictionary view is effectively a window 44 00:01:57,830 --> 00:02:00,670 into the dictionary so you're looking at 45 00:02:00,670 --> 00:02:05,200 that dictionary's data, and if you modify the dictionary 46 00:02:05,200 --> 00:02:08,680 after you acquire the view, the next time you iterate 47 00:02:08,680 --> 00:02:11,870 through the view, you'll see the modified contents. 48 00:02:11,870 --> 00:02:14,500 So let me demonstrate what I'm talking about here. 49 00:02:14,500 --> 00:02:17,490 Up above, we iterated immediately through 50 00:02:17,490 --> 00:02:19,590 whatever keys returned, but let's say 51 00:02:19,590 --> 00:02:22,210 we store that into a variable. 52 00:02:22,210 --> 00:02:25,090 So now we have this object which is a view 53 00:02:25,090 --> 00:02:27,470 into the months dictionary. 54 00:02:27,470 --> 00:02:29,400 And let's iterate through that, 55 00:02:29,400 --> 00:02:32,320 because we wanna just prove that we can. 56 00:02:32,320 --> 00:02:34,550 So... whoops, sorry about that. 57 00:02:34,550 --> 00:02:36,410 So if I go ahead and execute that, 58 00:02:36,410 --> 00:02:39,230 you see that indeed, January, February, and March 59 00:02:39,230 --> 00:02:41,410 are displayed as they were up above. 60 00:02:41,410 --> 00:02:43,380 The only difference between snippet two 61 00:02:43,380 --> 00:02:45,500 versus snippets four and five is that 62 00:02:45,500 --> 00:02:48,230 I took the intermediate step of creating the 63 00:02:48,230 --> 00:02:50,210 variable months view. 64 00:02:50,210 --> 00:02:54,430 So now, if we go modify the months dictionary, 65 00:02:54,430 --> 00:02:58,340 let's say we go and say months sub December 66 00:02:59,740 --> 00:03:04,120 and we give it the value 12, now of course 67 00:03:04,120 --> 00:03:06,440 the months dictionary has been modified 68 00:03:07,600 --> 00:03:10,120 and we can see the updated dictionary 69 00:03:10,120 --> 00:03:12,450 with four key value pairs. 70 00:03:12,450 --> 00:03:15,090 So at this point of course, if we were to get 71 00:03:15,090 --> 00:03:17,580 months dot keys or months dot values 72 00:03:17,580 --> 00:03:19,890 and iterate through them like we did up above, 73 00:03:19,890 --> 00:03:23,160 we would expect to see four items get displayed 74 00:03:23,160 --> 00:03:24,340 in each case. 75 00:03:24,340 --> 00:03:26,170 But what about when I iterate through 76 00:03:26,170 --> 00:03:29,620 months underscore view, which was acquired 77 00:03:29,620 --> 00:03:33,940 when the months dictionary only had three keys in it? 78 00:03:33,940 --> 00:03:35,570 Well, let's see what happens. 79 00:03:35,570 --> 00:03:40,070 So, for each of those keys, we're going to display them. 80 00:03:40,070 --> 00:03:42,340 So for each key in the months view, 81 00:03:42,340 --> 00:03:45,510 display the key followed by a couple of spaces. 82 00:03:45,510 --> 00:03:47,130 And when I go ahead and do that, 83 00:03:47,130 --> 00:03:51,270 notice that, indeed, we get four items being output, 84 00:03:51,270 --> 00:03:53,720 even though we're iterating through the months view, 85 00:03:53,720 --> 00:03:56,270 which was created back up here in snippet four 86 00:03:56,270 --> 00:03:58,390 when we still only had three keys, 87 00:03:58,390 --> 00:04:00,930 so this helps prove that in fact 88 00:04:00,930 --> 00:04:04,990 the view is looking at the original dictionary's data, 89 00:04:04,990 --> 00:04:08,923 it doesn't maintain its own copy of that data. 90 00:04:09,850 --> 00:04:14,530 Okay, so now that we've seen the keys, values 91 00:04:14,530 --> 00:04:16,430 and items methods and talked a little bit 92 00:04:16,430 --> 00:04:20,220 about dictionary views, sometimes you'll want to 93 00:04:20,220 --> 00:04:23,210 acquire either the keys, the values, or the full 94 00:04:23,210 --> 00:04:27,270 item tuples to place them into a list for example. 95 00:04:27,270 --> 00:04:30,750 So in Python it's real simple to do things like that, 96 00:04:30,750 --> 00:04:32,550 I have the built-in list function 97 00:04:32,550 --> 00:04:34,870 and I can say months dot keys 98 00:04:34,870 --> 00:04:37,950 and it will simply walk through those keys 99 00:04:37,950 --> 00:04:40,050 and insert them into the list 100 00:04:40,050 --> 00:04:42,220 and return the new list object. 101 00:04:42,220 --> 00:04:45,860 I can of course easily do the same thing with values, 102 00:04:45,860 --> 00:04:48,770 and if you're curious, we can also do 103 00:04:48,770 --> 00:04:51,450 the same thing with items in which case, 104 00:04:51,450 --> 00:04:54,260 we see the actual tuple objects 105 00:04:54,260 --> 00:04:56,830 that have been placed into the list 106 00:04:56,830 --> 00:04:59,900 that was being created in snippet 11 here. 107 00:04:59,900 --> 00:05:02,510 So this just further proves that the items method 108 00:05:02,510 --> 00:05:07,200 really does give us access to tuples of key value pairs. 109 00:05:07,200 --> 00:05:10,460 So at this point, let's just take a look 110 00:05:10,460 --> 00:05:12,920 at one other thing that you may find handy 111 00:05:12,920 --> 00:05:13,900 once in a while. 112 00:05:13,900 --> 00:05:16,280 Sometimes it's useful to iterate 113 00:05:16,280 --> 00:05:20,750 through a dictionary's keys in sorted order. 114 00:05:20,750 --> 00:05:24,690 So in the case of our keys, these are strings, 115 00:05:24,690 --> 00:05:27,440 so sorted order would basically mean 116 00:05:27,440 --> 00:05:29,370 listing these alphabetically, 117 00:05:29,370 --> 00:05:33,270 remember, however, that when you compare strings in Python, 118 00:05:33,270 --> 00:05:36,260 it's based on the underlying numeric values 119 00:05:36,260 --> 00:05:37,890 of the characters, so it's really a 120 00:05:37,890 --> 00:05:40,110 lexicographical comparison. 121 00:05:40,110 --> 00:05:43,580 Now in this case, each of the four keys 122 00:05:43,580 --> 00:05:45,760 starts with a capital first letter, 123 00:05:45,760 --> 00:05:48,140 so based on that, we would expect 124 00:05:48,140 --> 00:05:50,580 that these will be sorted alphabetically 125 00:05:50,580 --> 00:05:53,230 if we iterate through them in that order. 126 00:05:53,230 --> 00:05:56,470 So let me go ahead and copy one additional 127 00:05:56,470 --> 00:05:58,760 loop here and paste it in. 128 00:05:58,760 --> 00:06:01,090 In this case, we're going to take the result 129 00:06:01,090 --> 00:06:02,980 of the keys method and hand it off 130 00:06:02,980 --> 00:06:05,270 to that built-in function sorted 131 00:06:05,270 --> 00:06:09,220 that we demonstrated with lists in the preceding lesson. 132 00:06:09,220 --> 00:06:11,690 So it's going to acquire the keys, 133 00:06:11,690 --> 00:06:14,710 give me back an iterator that walks 134 00:06:14,710 --> 00:06:17,130 through those keys in sorted order, 135 00:06:17,130 --> 00:06:19,530 whatever sorted order means for the keys, 136 00:06:19,530 --> 00:06:22,050 in this case lexicographical order 137 00:06:22,050 --> 00:06:24,840 because they're strings, and for each of those month names, 138 00:06:24,840 --> 00:06:27,210 we want to display them and in fact, 139 00:06:27,210 --> 00:06:29,840 we get alphabetically now December, 140 00:06:29,840 --> 00:06:31,840 February, January, and March. 141 00:06:31,840 --> 00:06:34,670 Now you may not want to walk through month names 142 00:06:34,670 --> 00:06:38,060 in that order, but you might be, for example, 143 00:06:38,060 --> 00:06:42,460 keeping track of word counts in a body of text 144 00:06:42,460 --> 00:06:46,430 and what you might want to do is order the words 145 00:06:46,430 --> 00:06:48,600 alphabetically so that you can then 146 00:06:48,600 --> 00:06:52,350 see by word easily what each count was. 147 00:06:52,350 --> 00:06:56,670 Or maybe you want to order them not alphabetically 148 00:06:56,670 --> 00:06:59,160 but by the values of those keys, 149 00:06:59,160 --> 00:07:01,690 which would be the counts of the keys, 150 00:07:01,690 --> 00:07:03,410 and there's lots of things like that 151 00:07:03,410 --> 00:07:05,130 that you'll encounter as you start 152 00:07:05,130 --> 00:07:10,130 working with larger bodies of data stored in dictionaries.