1 00:00:00,000 --> 00:00:02,730 Let's take a look at another data type called a set. 2 00:00:02,900 --> 00:00:07,500 Now, a set is actually a lot less common than a tuple or a list, 3 00:00:08,160 --> 00:00:10,800 and that has to do with the fact that they are unique, 4 00:00:11,040 --> 00:00:14,310 and they also don't hold their order very well. 5 00:00:14,620 --> 00:00:18,190 And holding order can be very important in a program. 6 00:00:18,420 --> 00:00:22,860 So if I told you three things, three colors - red, green, 7 00:00:22,860 --> 00:00:26,850 and blue, and then I asked you 10 minutes from now, what 8 00:00:26,860 --> 00:00:27,750 were those three colors? 9 00:00:27,760 --> 00:00:29,310 And you said - blue, green, red. 10 00:00:29,310 --> 00:00:34,280 And I said, "Yes, but I was looking for a proper order". 11 00:00:34,660 --> 00:00:38,860 Now, as an example of keeping data sort of in order, with 12 00:00:38,870 --> 00:00:41,950 a list you could say operations of driving a car, 13 00:00:41,960 --> 00:00:44,050 and this is what you have to do in this order. 14 00:00:44,300 --> 00:00:51,380 You have to unlock the car, you have to get in the car, you 15 00:00:51,380 --> 00:00:56,800 have to start the car, and then you have to drive away in your car. 16 00:00:56,830 --> 00:01:01,670 And if I asked you if at any point in time I could drive 17 00:01:01,680 --> 00:01:05,200 a car, the order of operations for a car is - I want to drive 18 00:01:05,209 --> 00:01:06,700 away, then I want to get in the car, 19 00:01:06,709 --> 00:01:07,810 then I want to start the car, 20 00:01:07,820 --> 00:01:09,010 then I want to unlock the car, 21 00:01:09,020 --> 00:01:10,270 that's not going to make any sense. 22 00:01:10,380 --> 00:01:12,750 And that's why a lot of people don't use sets. 23 00:01:12,760 --> 00:01:15,450 Also, the fact that a lot of people actually don't know that sets 24 00:01:15,460 --> 00:01:19,110 even exist. Sets definitely have their place. 25 00:01:19,400 --> 00:01:22,200 So I'm just going to comment this out because that's not 26 00:01:22,200 --> 00:01:26,100 useful. And let's go ahead and create our first set. 27 00:01:26,100 --> 00:01:28,140 And what do we want a set of? 28 00:01:28,520 --> 00:01:30,170 Let's do a set of 'colors'. 29 00:01:30,170 --> 00:01:32,300 So a set looks like this. It's a variable. 30 00:01:32,540 --> 00:01:36,650 The variable name rather, is '=', and then opening and 31 00:01:36,660 --> 00:01:37,730 closing curly brackets. 32 00:01:37,740 --> 00:01:40,790 Now, this looks a lot like a dictionary, but a dictionary 33 00:01:40,800 --> 00:01:44,540 has a key and a value, a set does not have that. 34 00:01:44,540 --> 00:01:46,300 It simply has values. 35 00:01:46,380 --> 00:01:53,540 So I could say '', I guess, 36 00:01:53,800 --> 00:01:55,600 and that's fine. This is going to be a set. 37 00:01:55,860 --> 00:01:59,980 And if I sort of display this, we have 'Blue'. 38 00:02:00,010 --> 00:02:01,630 Here's a good example. 39 00:02:01,640 --> 00:02:04,150 I said, 'Blue', 'Red', 'Yellow', 'Orange', 'Green'. 40 00:02:04,440 --> 00:02:07,110 Python said, "I don't care about storing the order". 41 00:02:07,380 --> 00:02:09,870 So 'Blue', 'Green', 'Orange', 'Red', 'Yellow'. 42 00:02:10,380 --> 00:02:13,710 And these do not line up whatsoever. 43 00:02:13,940 --> 00:02:16,670 So that's a good example of sets not keeping their order. 44 00:02:16,940 --> 00:02:21,800 The other thing that makes a set very unique is the fact 45 00:02:21,810 --> 00:02:23,630 that all the items have to be unique. 46 00:02:23,820 --> 00:02:28,500 So if I change this to 'Green', so there's two Greens in here 47 00:02:28,660 --> 00:02:33,700 and there's two 'Reds', and there's two 'Blues', and I rerun this, 48 00:02:33,700 --> 00:02:37,300 and then I rerun this. Well, the order changed, 49 00:02:37,630 --> 00:02:39,250 but these are all still unique. 50 00:02:39,260 --> 00:02:42,490 There's still only ever 'Blue', 'Green', 'Orange', 'Yellow', and 51 00:02:42,500 --> 00:02:45,460 'Red'. Now let's go ahead and add something to this. So we can do 52 00:02:45,470 --> 00:02:49,530 'colors.', hit tab, and we can see all sorts of stuff. 53 00:02:49,540 --> 00:02:50,610 Now in a list, 54 00:02:50,620 --> 00:02:54,090 we appended to a list. Because a list is in order 55 00:02:54,100 --> 00:02:56,040 we have to append to the back of it. 56 00:02:56,300 --> 00:02:59,330 With a set, we don't have to append to the back of it because 57 00:02:59,340 --> 00:03:02,060 it doesn't need to keep its order. 58 00:03:02,070 --> 00:03:03,320 So we're just going to add to it. 59 00:03:03,330 --> 00:03:05,990 And wherever Python says that data point gets stored, is where 60 00:03:06,000 --> 00:03:07,610 it gets stored because we don't care. 61 00:03:07,610 --> 00:03:11,370 We just care that the items are unique and that we have a 62 00:03:11,380 --> 00:03:13,710 sequence that we can eventually loop through these. 63 00:03:13,860 --> 00:03:17,990 So 'color.add', and let's add "Violet". 64 00:03:18,000 --> 00:03:19,580 We'll add "Violet" in there. 65 00:03:20,060 --> 00:03:23,720 And once again, sets are mutable, so you do not need to be 66 00:03:24,000 --> 00:03:26,400 like 'colors = colors.add' 67 00:03:26,600 --> 00:03:30,100 You can just do 'colors.add', and it will do it internally for us. 68 00:03:30,180 --> 00:03:34,350 So if we type 'colors' again, we can see we've added, where 69 00:03:34,350 --> 00:03:35,300 is "Violet"? Right there. 70 00:03:35,360 --> 00:03:39,200 And I wonder, just for funsies if I display this out again, 71 00:03:39,420 --> 00:03:43,670 I know it maintains its order this time, but let's see if 72 00:03:43,680 --> 00:03:45,050 I add "Violet" again. 73 00:03:45,050 --> 00:03:47,800 Nothing's going to happen because it's already unique, so nothing has changed. 74 00:03:47,850 --> 00:03:50,480 Python was probably smart enough to not ever actually change 75 00:03:50,510 --> 00:03:53,820 anything. But let's go ahead and add "Violet" once more. 76 00:03:54,060 --> 00:03:57,810 So I'm going to copy that and put that here, and do 'colors' 77 00:03:58,080 --> 00:03:59,820 I didn't add it. 78 00:03:59,830 --> 00:04:02,130 If this was a list, "Violet" would be in there twice. 79 00:04:02,280 --> 00:04:06,090 Now let's go ahead and remove something. So we can just do 80 00:04:06,100 --> 00:04:09,450 'colors.', and then ;'difference', 'discard', 'intersection', 81 00:04:09,460 --> 00:04:10,740 that is a very nice feature. 82 00:04:10,750 --> 00:04:11,850 We won't get into that yet. 83 00:04:11,880 --> 00:04:14,910 We can 'pop' just like in a list, but we also have 'remove'. 84 00:04:14,920 --> 00:04:17,540 So let's 'remove('Orange')'. 85 00:04:17,550 --> 00:04:20,630 Not that I have anything against Orange, but let's just get 86 00:04:20,640 --> 00:04:24,760 rid of it. 'colors', and it is gone, just like that. 87 00:04:24,769 --> 00:04:27,490 So it is a lot like a list in the sense that you can add 88 00:04:27,500 --> 00:04:28,780 something, you can remove something. 89 00:04:28,900 --> 00:04:31,510 It's just unique and does not maintain its order. 90 00:04:31,520 --> 00:04:34,600 So if you have an order of operations that's important, and 91 00:04:34,610 --> 00:04:36,310 most of the time, you probably will, 92 00:04:36,560 --> 00:04:38,540 you'll want to use a list. 93 00:04:38,550 --> 00:04:42,530 If that list never ever needs to change, you'll want to 94 00:04:42,540 --> 00:04:47,490 use a tuple. If you have a bunch of data and it needs to be 95 00:04:47,500 --> 00:04:50,130 unique, but it does not matter what the order is, and there 96 00:04:50,140 --> 00:04:51,600 is a place in time for that, 97 00:04:51,800 --> 00:04:53,510 then you want to use a set. 98 00:04:53,630 --> 00:04:57,330 Now I'm going to show you a cool little trick here where 99 00:04:57,340 --> 00:05:00,960 we can create a list of duplicate items. 100 00:05:00,970 --> 00:05:07,170 So let's say 'Blue', 'Blue', 'Red', 'Red', 'Yellow', 101 00:05:07,320 --> 00:05:10,380 And 'lst' is going to say 'Blue', 'Blue', 'Red', 'Red', 'Yellow'. 102 00:05:10,380 --> 00:05:11,900 And we know that this is a list. 103 00:05:11,900 --> 00:05:16,200 [no audio] 104 00:05:16,200 --> 00:05:19,200 Oh, I skipped a thing that I've been doing as well. 105 00:05:19,210 --> 00:05:23,650 So let's see what the 'type' is of 'color' or 'colors'. 106 00:05:23,900 --> 00:05:26,500 This was a set. Python says, "Yep, 107 00:05:26,650 --> 00:05:27,730 sure enough. That's a set". 108 00:05:28,200 --> 00:05:30,800 Okay. So I want to make sure we covered that as well. 109 00:05:31,000 --> 00:05:33,820 So we have this list '['Blue', 'Blue', 'Red', 'Red', 'Yellow']', 110 00:05:33,830 --> 00:05:38,190 and let's say we don't care that the order is 'Blue', 'Blue', 111 00:05:38,200 --> 00:05:38,940 'Red', 'Red', 'Yellow'. 112 00:05:38,950 --> 00:05:40,710 We just care that these items are unique. 113 00:05:40,720 --> 00:05:44,100 What we can do, is we can say 'colors =' and this 114 00:05:44,110 --> 00:05:47,830 is called typecasting or variable casting in some languages. 115 00:05:47,840 --> 00:05:51,310 And when you variable cast, you're going to take all these 116 00:05:51,320 --> 00:05:53,290 items, and you're going to put it into a set. 117 00:05:53,300 --> 00:05:55,420 So you're converting it from a list to a set. 118 00:05:55,760 --> 00:06:00,560 So let's convert this list to a set, and we'll get into typecasting 119 00:06:00,560 --> 00:06:03,260 down the road, but this is actually something really cool that I think is 120 00:06:03,270 --> 00:06:05,330 very interesting and useful right away 121 00:06:05,390 --> 00:06:08,070 as a new programmer. Let's go ahead and type 'colors'. 122 00:06:08,070 --> 00:06:12,600 Now that has been cast as a set, we only have 'Blue', 'Red', 'Yellow'. 123 00:06:12,640 --> 00:06:15,180 Again, we don't care about the order that it is. 124 00:06:15,190 --> 00:06:16,770 It could be 'Yellow', 'Red', 'Blue', 125 00:06:16,770 --> 00:06:20,100 It could be 'Yellow', 'Blue', 'Red'; 'Blue', 'Red', 'Yellow'; 'Blue', 'Yellow', 126 00:06:20,100 --> 00:06:21,400 'Red', doesn't really matter. 127 00:06:21,520 --> 00:06:24,160 And let's once again cast that. 128 00:06:24,500 --> 00:06:29,200 So we changed this list, which is very clearly a list data type to a set. 129 00:06:29,220 --> 00:06:32,430 Now, as an example, just to loop through all of these 'colors', 130 00:06:32,440 --> 00:06:36,230 what we can do is, say 'for color in colors', 131 00:06:36,980 --> 00:06:38,510 just simply 'print(color)'. 132 00:06:38,520 --> 00:06:41,270 So it's going to loop through every item in the sequence. 133 00:06:41,270 --> 00:06:45,200 It's going to say, "Hey, there's a Blue, there's Red, there's a Yellow". 134 00:06:45,260 --> 00:06:48,260 But remember, it might not actually keep that order at all. 135 00:06:48,540 --> 00:06:49,980 So let's go ahead and run that. 136 00:06:49,990 --> 00:06:51,270 Sure enough, it didn't. 137 00:06:51,280 --> 00:06:53,310 It went 'Yellow', 'Red', 'Blue' 138 00:06:53,320 --> 00:06:55,800 And again, if that order of operations doesn't matter, and 139 00:06:55,810 --> 00:06:58,800 you want that list to be unique, a set is a good way to go.