1 00:00:00,410 --> 00:00:04,610 Well let's talk about set comprehension and dictionary comprehension. 2 00:00:06,330 --> 00:00:13,890 Now this first one is actually quite easy to see because we can do the same thing that we've done with 3 00:00:13,890 --> 00:00:14,550 lists. 4 00:00:14,550 --> 00:00:23,040 We simply change these bracketed notation to a set which is a curly bracket like this so that if I do 5 00:00:23,430 --> 00:00:29,400 my list the first one I get a set. 6 00:00:29,430 --> 00:00:30,790 Same with the second one. 7 00:00:30,990 --> 00:00:33,970 If I go like this my list to 8 00:00:36,700 --> 00:00:40,690 just like that and I think you get the idea let's do one more on the final one. 9 00:00:40,720 --> 00:00:41,800 So my list for 10 00:00:45,060 --> 00:00:50,640 again you're able to do set comprehension on sets as well. 11 00:00:50,640 --> 00:00:58,350 A quick way for us to generate sets and remember sets only allow numbers that are not duplicate only 12 00:00:58,350 --> 00:01:00,600 unique items. 13 00:01:00,660 --> 00:01:04,070 What about dictionary comprehension. 14 00:01:04,260 --> 00:01:16,280 Well let's do something like this using the same formula we've used let's create a my dict and then 15 00:01:16,280 --> 00:01:29,030 here we're going to create a key and a value just like that and this key value can be acted upon. 16 00:01:29,080 --> 00:01:36,100 So in our case let's say we want to multiply the value by two or to the power of two. 17 00:01:36,110 --> 00:01:41,140 So this is what we want to create a key value pair. 18 00:01:41,300 --> 00:01:44,650 And the value gets acted upon now. 19 00:01:44,680 --> 00:01:48,990 In here I'm going to say another four loop four. 20 00:01:49,030 --> 00:02:01,040 So let's say key value and then in well and what what are we going to iterate over well let's think 21 00:02:01,040 --> 00:02:02,030 here. 22 00:02:02,030 --> 00:02:07,970 In order for us to create a dictionary we need both a key and a value. 23 00:02:07,970 --> 00:02:08,600 Right. 24 00:02:08,600 --> 00:02:15,770 So I need to pass it and it's horrible but I need to pass it an extra bowl like a dictionary that has 25 00:02:15,770 --> 00:02:27,700 both key and value so we can do something simple here we can say simple dictionary that will have a 26 00:02:28,600 --> 00:02:40,390 as one and B as two and now all we do is pass a simple dictionary in here as an admirable. 27 00:02:40,710 --> 00:02:46,850 We grab the key and values by doing dot items which we've seen before. 28 00:02:46,850 --> 00:02:48,900 And let's make this a little bit bigger. 29 00:02:50,190 --> 00:02:50,670 There you go 30 00:02:53,730 --> 00:02:56,440 the it all the way just so we're all clear. 31 00:02:56,560 --> 00:03:04,510 It's all on one line and now let's just see what happens and we'll go through it step by step. 32 00:03:04,560 --> 00:03:15,150 I'm going to copy this and paste it in if I click Run. 33 00:03:15,270 --> 00:03:16,150 Look at that. 34 00:03:16,200 --> 00:03:22,170 I have a new dictionary that has a equal to 1 and b equal to 4. 35 00:03:22,170 --> 00:03:30,850 Now let's go through it one by one just like we saw with list and set the first part is what we want 36 00:03:30,850 --> 00:03:33,620 to do with the data. 37 00:03:33,750 --> 00:03:40,770 In our case I wanted key and value which I was going to extract here in our for loop. 38 00:03:40,770 --> 00:03:46,050 I could have named this whatever I want though I could have done K envy and you might see this commonly 39 00:03:46,110 --> 00:03:54,360 in Python code just short term for key and value and then I wanted to do the power of two to the value. 40 00:03:54,500 --> 00:03:57,380 So then this is what I wanted to do. 41 00:03:57,500 --> 00:03:58,880 This is the action. 42 00:03:58,880 --> 00:04:02,540 This is the for loop that we did. 43 00:04:02,540 --> 00:04:08,650 So the for loop was hey extract the key in value using the dictionary dot items. 44 00:04:08,660 --> 00:04:16,080 Remember I needed to have two values so I can fill in key and value and then the value you multiplied 45 00:04:16,080 --> 00:04:17,980 by two or two the power of two. 46 00:04:18,010 --> 00:04:27,100 So we get that if for example I wanted to add an if statement and say Hey I only want you to add if 47 00:04:27,550 --> 00:04:39,200 the value is even so let's say modulo two equals zero only then I want you to add the dictionary if 48 00:04:39,200 --> 00:04:45,450 I click Run only that even value for gets added to the dictionary. 49 00:04:45,560 --> 00:04:47,390 Very very cool. 50 00:04:47,450 --> 00:04:50,350 Again this makes your code a little bit less readable. 51 00:04:50,630 --> 00:04:57,470 So it's just a shorthand for you to use and maybe if you see it in code you at least know what it is 52 00:04:58,190 --> 00:05:04,700 but you should be careful not to be overly clever and do these single line dictionary comprehension 53 00:05:04,970 --> 00:05:08,770 just because you can't just solidify our knowledge. 54 00:05:08,810 --> 00:05:12,080 Let's finish up with another example. 55 00:05:12,220 --> 00:05:24,590 What if instead I have this I want to do a four number in a list. 56 00:05:24,590 --> 00:05:34,720 One two three so we no longer have this dictionary but we still need to include a key and a value pair. 57 00:05:34,720 --> 00:05:44,290 Could I do it so that maybe the item is the key and the item multiplied by two is the value. 58 00:05:44,290 --> 00:05:45,450 How would we do that. 59 00:05:45,490 --> 00:05:48,200 Pause the video and give it a go if you want. 60 00:05:48,220 --> 00:05:50,410 Otherwise this is how we do it. 61 00:05:50,590 --> 00:05:52,470 We simply say no. 62 00:05:52,660 --> 00:05:57,470 Is the key and the value will be Num Times two. 63 00:05:57,520 --> 00:05:58,650 Just like that. 64 00:05:58,780 --> 00:06:00,570 Nothing too difficult. 65 00:06:00,570 --> 00:06:01,410 Right. 66 00:06:01,480 --> 00:06:04,010 If I click Run. 67 00:06:04,220 --> 00:06:13,010 There we go we're able to use comprehension as list comprehension is set comprehension. 68 00:06:13,120 --> 00:06:20,980 Dictionary comprehension is as a way to create lists with python as a way to create these data structures 69 00:06:21,070 --> 00:06:23,580 really fast with Python. 70 00:06:23,670 --> 00:06:26,370 Let's take a break and do an exercise in the next video.