1 00:00:00,000 --> 00:00:02,220 Let's take a look at a dictionary comprehension. 2 00:00:02,230 --> 00:00:05,850 So a dictionary comprehension is a lot like a list comprehension, 3 00:00:06,020 --> 00:00:11,030 but instead of having a for loop that could be 3, 4, 10 4 00:00:11,040 --> 00:00:14,570 lines of code, we have it basically set up in one line of 5 00:00:14,570 --> 00:00:17,400 code. So let's go through the long way, and then let's go 6 00:00:17,400 --> 00:00:20,830 through the short way, which is a dictionary comprehension. 7 00:00:20,840 --> 00:00:26,220 So as an example, let's say we have a list of 'keys', and we've 8 00:00:26,220 --> 00:00:29,600 got a 'name', and 'age', and a 'role'; 9 00:00:30,240 --> 00:00:32,640 and then we've got a list of 'values' in here as well. 10 00:00:33,800 --> 00:00:39,100 And I'm going to put in just my information here, and I am a 'Teacher'. 11 00:00:39,100 --> 00:00:42,800 So what I could do, is I could create an empty dictionary called 'person', 12 00:00:42,850 --> 00:00:47,790 and I could say 'for key, value in zip(keys, values)', 13 00:00:47,790 --> 00:00:51,300 Remember, 'zip' is going to take these two lists and merge 14 00:00:51,330 --> 00:00:53,220 them together in a paired tuple. 15 00:00:53,220 --> 00:00:56,800 So it's going to be - 'name' with 'Kalob'; 'age' - 30; 'role' as 'Teacher'. 16 00:00:56,800 --> 00:01:01,800 And then I could say, 'person[key] = value', 17 00:01:03,100 --> 00:01:07,400 and when we print this out, we have a dictionary 'person' with 18 00:01:07,400 --> 00:01:11,100 the key of 'name' is 'Kalob'; key of 'age' - 30; key of 'role' is 'Teacher'. 19 00:01:11,100 --> 00:01:13,860 And let's just verify this is in fact a dictionary. 20 00:01:13,870 --> 00:01:17,160 We can see that it is by how its syntax looks, just like this. 21 00:01:17,170 --> 00:01:20,320 But just to be fair, we know that it's a dictionary. 22 00:01:20,560 --> 00:01:22,570 Now, this is a very small example, 23 00:01:22,570 --> 00:01:26,700 but what we could do instead is put this all into one line. 24 00:01:26,700 --> 00:01:27,700 Well, not all of it. 25 00:01:27,770 --> 00:01:29,200 We can't put this into one line. 26 00:01:29,210 --> 00:01:31,870 Those need to be pre-existing because they're variables we're going 27 00:01:31,870 --> 00:01:33,800 to use inside of the 'zip' function. 28 00:01:33,860 --> 00:01:37,640 So what we could do here, is let's say 'person2' so that 29 00:01:37,650 --> 00:01:39,650 we don't overwrite the original 'person' here. 30 00:01:39,660 --> 00:01:42,650 'person2 = {', 31 00:01:42,660 --> 00:01:44,960 and what I'm going to do is actually put this on a new line, 32 00:01:44,970 --> 00:01:47,270 just because I like to put this on a new line sometimes to 33 00:01:47,280 --> 00:01:48,350 make it a little more readable. 34 00:01:48,360 --> 00:01:50,690 And then I'll put it on a single line 35 00:01:50,700 --> 00:01:54,320 if it's short enough. If your list comprehensions or your 36 00:01:54,330 --> 00:01:57,050 dictionary comprehensions ever get too long, feel free to 37 00:01:57,050 --> 00:01:58,700 put them on multiple lines, that's okay. 38 00:01:58,700 --> 00:02:01,900 And so what we want to do here is write our for loop. 39 00:02:01,900 --> 00:02:07,000 So 'for key, value in zip(keys, values)', 40 00:02:07,300 --> 00:02:12,000 Now, in a list comprehension, we would really just say 'key', 41 00:02:12,000 --> 00:02:14,400 'key for key, value in zip', 42 00:02:14,480 --> 00:02:15,860 and this is what it's returning. 43 00:02:15,870 --> 00:02:19,330 But a dictionary has a 'key' and a 'value' in it, and you can 44 00:02:19,340 --> 00:02:21,910 actually see that it's got a little cool in here. 45 00:02:21,920 --> 00:02:23,440 So this is your 'key', this is your 'value'. 46 00:02:23,440 --> 00:02:26,300 We do the same thing in a dictionary comprehension. 47 00:02:26,300 --> 00:02:27,600 'key: value'. 48 00:02:28,100 --> 00:02:31,100 And let's go ahead and just put this back all on one line. 49 00:02:31,100 --> 00:02:38,100 And let's run this, 'person2', it's the exact same thing as what 50 00:02:38,100 --> 00:02:39,100 we wrote up here. 51 00:02:39,900 --> 00:02:42,500 Now we could even add 'if' statements as well. 52 00:02:42,500 --> 00:02:45,300 So let's go ahead and create 'person3', 53 00:02:45,300 --> 00:02:47,300 but let's say we didn't want the 'role' in there. 54 00:02:48,300 --> 00:02:53,300 We could say 'if', at the very end of this, the 'key == 55 00:02:53,300 --> 00:02:56,300 age', we could get just the 'age', or we could do the inverse 56 00:02:56,300 --> 00:03:00,800 and we could say, only ever do this if the 'key != age'. 57 00:03:00,800 --> 00:03:04,800 So it's just going to return 'name' is 'Kalob', and 'role' is 'Teacher'. 58 00:03:04,800 --> 00:03:07,000 We're going to jam that into person number three, 59 00:03:07,000 --> 00:03:09,000 [no audio] 60 00:03:09,000 --> 00:03:11,000 and 'person3'. Bam. 61 00:03:11,400 --> 00:03:16,200 We have now just looped over 'keys' and 'values' in two different 62 00:03:16,200 --> 00:03:19,100 lists, merged them together, and down here 63 00:03:19,100 --> 00:03:25,300 what we said was, if that key, if that key up here is 'age', skip it. 64 00:03:25,350 --> 00:03:28,900 Otherwise, if the key is anything else, if the key equals 65 00:03:28,930 --> 00:03:33,130 literally anything else that does not match 'age', then add 66 00:03:33,140 --> 00:03:34,120 it to your dictionary. 67 00:03:34,130 --> 00:03:36,730 And that is a dictionary comprehension. 68 00:03:36,730 --> 00:03:39,900 So what I would like you to do is get a little bit of experience with this one. 69 00:03:39,950 --> 00:03:44,030 Dictionary comprehensions are a little less commonplace than 70 00:03:44,030 --> 00:03:48,300 a list comprehension, but it's still wildly valuable in Python, 71 00:03:48,300 --> 00:03:50,500 and you're going to run into these anyway, so you might as 72 00:03:50,530 --> 00:03:51,900 well get some practice with it now. 73 00:03:51,900 --> 00:03:54,000 So go ahead, give this a shot. 74 00:03:54,000 --> 00:03:56,400 Don't forget, you can always download this Jupyter Notebook as well, and 75 00:03:56,400 --> 00:03:57,600 play around with my code. 76 00:03:57,600 --> 00:04:00,400 And once you think you have the hang of it, let's go ahead 77 00:04:00,400 --> 00:04:01,700 and start that next lesson.