WEBVTT 1 00:00:02.280 --> 00:00:05.830 So the last of Python's data structures that we're gonna look at 2 00:00:05.830 --> 00:00:08.630 in this part of the course, is sets. 3 00:00:08.630 --> 00:00:11.660 Now if you're familiar with mathematical set theory, 4 00:00:11.660 --> 00:00:14.000 then Python's sets will make immediate sense to you. 5 00:00:14.000 --> 00:00:17.920 Now, a set in Python is unordered and doesn't contain duplicates, so 6 00:00:17.920 --> 00:00:20.500 in that respect it's similar to a dictionary. 7 00:00:20.500 --> 00:00:24.590 However, unlike a dictionary, item's aren't accessed via a key. 8 00:00:24.590 --> 00:00:28.760 In fact, a set is probably more similar to a collection of dictionary keys. 9 00:00:28.760 --> 00:00:32.490 And the set members are hashed in the same ways dictionary keys are. 10 00:00:32.490 --> 00:00:35.240 Now, the elements in a set must be immutable objects. 11 00:00:35.240 --> 00:00:37.070 Again, just like dictionary keys. 12 00:00:37.070 --> 00:00:38.530 They also support the union and 13 00:00:38.530 --> 00:00:40.950 intersection operations that can be performed. 14 00:00:40.950 --> 00:00:41.970 On sets. 15 00:00:41.970 --> 00:00:45.180 Now sets are generally used less often than the other data structures we've 16 00:00:45.180 --> 00:00:49.690 looked at so far, but that said, they can be very useful for cleaning up data. 17 00:00:49.690 --> 00:00:53.380 And we'll look at that after we've seen how to create and manipulate some sets. 18 00:00:53.380 --> 00:00:54.300 Here in Python 3, 19 00:00:54.300 --> 00:00:59.290 actually since Python 2.6, you can define a set by using curly braces. 20 00:00:59.290 --> 00:01:00.930 Which is the same way you define a dictionary. 21 00:01:00.930 --> 00:01:03.440 So let's go through it doing it two ways so 22 00:01:03.440 --> 00:01:05.160 you can see the two ways to create sets. 23 00:01:05.160 --> 00:01:10.690 I'm gonna start with farm 24 00:01:10.690 --> 00:01:16.002 animals equals 25 00:01:16.002 --> 00:01:21.380 [NOISE] Run that first of all. 26 00:01:23.660 --> 00:01:24.750 Nothing particularly new there. 27 00:01:26.290 --> 00:01:31.692 And, of course we can go for animal in farm_animals, 28 00:01:31.692 --> 00:01:35.790 print(animal). 29 00:01:35.790 --> 00:01:43.000 Let's make a bit of a gap here, print And let's create a set a different way. 30 00:01:43.000 --> 00:01:47.147 So we'll do wild_animals = set. 31 00:01:47.147 --> 00:01:52.830 It's a built-in function and we'll add some, so a lion. 32 00:01:54.140 --> 00:02:00.402 Oops Lion, tiger, panther, 33 00:02:00.402 --> 00:02:05.887 elephant and hare. 34 00:02:05.887 --> 00:02:12.930 Hare print are animals and 35 00:02:12.930 --> 00:02:19.050 we can do the same for animal in wildlife animals, 36 00:02:19.050 --> 00:02:21.820 print animals. 37 00:02:21.820 --> 00:02:22.430 So I can run that. 38 00:02:25.790 --> 00:02:27.680 You can see the two ways to create sets. 39 00:02:27.680 --> 00:02:30.760 The first one is just again very similar to dictionary. 40 00:02:30.760 --> 00:02:35.910 The form for creating dictionary but we're not actually Supply key there, 41 00:02:35.910 --> 00:02:41.050 so that's how Python knows that this is in fact a set, and a second way, 42 00:02:41.050 --> 00:02:46.010 what we do is we actually pass the list to the set built-in function, and 43 00:02:46.010 --> 00:02:48.530 created a set that way. 44 00:02:48.530 --> 00:02:52.150 And not surprisingly, as you saw on the screen there using the For loop, 45 00:02:52.150 --> 00:02:54.390 we can iterate through the members of a set and 46 00:02:54.390 --> 00:02:57.700 then ultimately print them out as we saw on the example on the screen. 47 00:02:57.700 --> 00:03:00.950 Now, actually, adding a new member to a set is also pretty easy, 48 00:03:00.950 --> 00:03:03.190 using the add method, so let's go back up to here. 49 00:03:04.280 --> 00:03:06.020 So what we'll do is we'll start the int. 50 00:03:06.020 --> 00:03:07.950 So we'll type farm_animals. 51 00:03:07.950 --> 00:03:11.100 Let's add one for every set that we've created. 52 00:03:11.100 --> 00:03:15.630 Let's add a horse to our [NOISE] Animal set and then for 53 00:03:15.630 --> 00:03:18.295 our wild animals let's add a horse to that as well. 54 00:03:18.295 --> 00:03:25.160 [NOISE] Okay then we can just oops 55 00:03:27.660 --> 00:03:32.070 print farm animals print wild animals. 56 00:03:34.020 --> 00:03:38.410 Sign we can run that And, not surprisingly, you can see we now have 57 00:03:38.410 --> 00:03:42.930 the horse added both to the farm animal set and also to the wild animal set. 58 00:03:42.930 --> 00:03:47.360 And the other thing I want to show you there as well, if we just check the order 59 00:03:47.360 --> 00:03:50.860 because there is no inherent ordering with sets, as I mentioned. 60 00:03:50.860 --> 00:03:57.500 So if we run it again, Notice how the order of these are changing each time. 61 00:03:57.500 --> 00:04:02.120 And that's because of course the sets are actually un ordered. 62 00:04:02.120 --> 00:04:05.580 And they can't really confirm a particular order 63 00:04:05.580 --> 00:04:08.390 with sets because they are by their very nature un ordered. 64 00:04:08.390 --> 00:04:13.770 Now incidentally you work in an era to use [...] in the right hand margin. 65 00:04:13.770 --> 00:04:15.670 Set, function, method, call. 66 00:04:15.670 --> 00:04:18.860 That the set function call is used to create the wild set. 67 00:04:18.860 --> 00:04:19.570 The wild animals. 68 00:04:19.570 --> 00:04:21.345 I'll have you click on that and select it. 69 00:04:21.345 --> 00:04:24.095 It's basically saying you could use a literal for that. 70 00:04:24.095 --> 00:04:27.295 So we're just going or more information he'll be replaced with a literal. 71 00:04:27.295 --> 00:04:29.295 So it's just basically saying if use is set here but 72 00:04:29.295 --> 00:04:30.685 you could do something like that and 73 00:04:30.685 --> 00:04:34.395 replace it with that now there are cases where we have to use the set function. 74 00:04:34.395 --> 00:04:36.365 And one is if we want to create an empty set. 75 00:04:36.365 --> 00:04:41.215 We can't actually create an empty set using just the empty braces because that 76 00:04:41.215 --> 00:04:45.640 actually creates an empty dictionary So in that instance, we have to use set. 77 00:04:45.640 --> 00:04:50.670 And just to show you what I mean, we'll comment this code out for now. 78 00:04:50.670 --> 00:04:58.280 So we can start off by typing empty_set = set() and that creates an empty set. 79 00:04:58.280 --> 00:05:03.856 But if we try and do it this way, empty_set_2 = 80 00:05:05.650 --> 00:05:09.313 Like so, and we do empty_set.add, A, 81 00:05:09.313 --> 00:05:14.710 empty_set_2.add, noting there's no method there. 82 00:05:14.710 --> 00:05:18.850 If we run this, we get an error, 83 00:05:18.850 --> 00:05:23.560 and just to confirm, we can see the error should be on line 22. 84 00:05:23.560 --> 00:05:25.960 You can see it's on line 22 there. 85 00:05:25.960 --> 00:05:30.560 And that's because the line on 20, it actually created a dictionary. 86 00:05:30.560 --> 00:05:32.790 Which, of course, doesn't have an add method. 87 00:05:32.790 --> 00:05:34.320 So that's why you need to use the set there. 88 00:05:34.320 --> 00:05:38.210 If you were creating an empty set, you'd actually physically need to call 89 00:05:38.210 --> 00:05:41.320 the in-built function for set to create that for you. 90 00:05:41.320 --> 00:05:44.530 And when we used the set constructor, when we used it earlier. 91 00:05:44.530 --> 00:05:46.620 But you can see online 9 which I've commented out. 92 00:05:46.620 --> 00:05:52.120 We passed the list parameter but you can actually use any [.] object 93 00:05:52.120 --> 00:05:57.510 such as a [.] or a range to do the same thing so what we do is something like and 94 00:05:57.510 --> 00:06:01.330 even equals set, range, 042. 95 00:06:01.330 --> 00:06:05.320 Remember that from our discussion of ranges. 96 00:06:05.320 --> 00:06:05.820 Print even. 97 00:06:07.010 --> 00:06:07.590 We can run that. 98 00:06:08.740 --> 00:06:10.060 Whoops, I'll need to get rid of that other error, 99 00:06:10.060 --> 00:06:14.640 so I'll just comment that second add out, and then run that again. 100 00:06:14.640 --> 00:06:19.680 You can see that's created a set based on those numbers, based on the range there. 101 00:06:19.680 --> 00:06:26.932 And we're gonna do something like squares_tuple. 102 00:06:26.932 --> 00:06:31.772 = (4,6,9, 16, 25) and squares = set 103 00:06:31.772 --> 00:06:36.830 (squares_turple) then print (squares), so 104 00:06:36.830 --> 00:06:42.850 I can do it that way, 105 00:06:42.850 --> 00:06:48.050 and you can see me correctly get a set there as well So when you're 106 00:06:48.050 --> 00:06:52.590 creating sets from tuples, ranges, lists, or literally any other type of object, 107 00:06:52.590 --> 00:06:56.490 you have to use the set constructor, rather than using curly braces. 108 00:06:56.490 --> 00:06:59.030 Now obviously in this simple example I've got on screen, 109 00:06:59.030 --> 00:07:02.440 we could do away with the tuple and just enter the numbers within curly braces, but 110 00:07:02.440 --> 00:07:06.120 the example is showing you that a tuple can be used to generate a set. 111 00:07:06.120 --> 00:07:07.820 And it's most likely you'd probably, at some point, 112 00:07:07.820 --> 00:07:09.460 be wanting to do that when you're programming in Path. 113 00:07:09.460 --> 00:07:13.510 So hopefully, now you're getting an idea, seeing these numbers consistently come up. 114 00:07:13.510 --> 00:07:16.810 There really is no guarantee that any item's gonna be printed in order, 115 00:07:16.810 --> 00:07:20.520 and it's really emphasized the unordered nature of sets in general. 116 00:07:20.520 --> 00:07:23.420 And as you saw in the first example with the even numbers 117 00:07:23.420 --> 00:07:25.340 They're not even necessarily appearing in order, 118 00:07:25.340 --> 00:07:27.300 even though they were actually generated in order. 119 00:07:27.300 --> 00:07:28.780 A very important point there. 120 00:07:28.780 --> 00:07:32.220 So, unfortunately their lack of an order makes it slightly harder to see 121 00:07:32.220 --> 00:07:32.810 what's going on. 122 00:07:32.810 --> 00:07:36.580 But, let's continue on anyway, and have a look at it, now using union, intersection, 123 00:07:36.580 --> 00:07:38.340 and difference, as well as subsets. 124 00:07:38.340 --> 00:07:41.150 So, what we're gonna start with is using union. 125 00:07:41.150 --> 00:07:46.540 So, I'm just gonna comment this other code out Union being a function of sets. 126 00:07:48.550 --> 00:07:56.300 So we'll put our even in again so even equals set range, so we'll write 42 again. 127 00:07:57.750 --> 00:08:03.510 Print even, print len even to establish how many entries there are, 128 00:08:03.510 --> 00:08:05.330 then we'll put squares. 129 00:08:06.820 --> 00:08:12.760 Probably should of just copied and pasted some of these. 130 00:08:12.760 --> 00:08:16.185 Squares underscore tuple equals 4, 6, 9, 16, 25 again. 131 00:08:16.185 --> 00:08:22.960 Squares = set(squares_tuple). 132 00:08:22.960 --> 00:08:26.180 And we'll just do a print(squares) and 133 00:08:26.180 --> 00:08:29.236 you'll see why we're doing this in a minute. 134 00:08:29.236 --> 00:08:33.247 Squares_tuple, print(squares) and 135 00:08:33.247 --> 00:08:39.230 print(len(squares)) because the number of elements is going to make sense and 136 00:08:39.230 --> 00:08:40.260 it's going to be important shortly. 137 00:08:42.510 --> 00:08:45.950 Now, if we go ahead and introduce the union commands, we're gonna talk print, 138 00:08:48.960 --> 00:08:53.789 even dot union squares, and 139 00:08:53.789 --> 00:09:02.147 print len Even.union(squares). 140 00:09:02.147 --> 00:09:07.250 So if you run that, and scroll this across, so 141 00:09:07.250 --> 00:09:11.800 that you can see that the new set that we've created on line 38 and 142 00:09:11.800 --> 00:09:15.100 39 now contains the numbers 9 and 25. 143 00:09:15.100 --> 00:09:18.210 And they obviously weren't in the original set of even numbers. 144 00:09:18.210 --> 00:09:21.920 And incidentally that's exactly the same set of squares dot union even. 145 00:09:21.920 --> 00:09:24.350 So we're working back the other way just to confirm that. 146 00:09:26.120 --> 00:09:30.290 We can also do a print squares.union 147 00:09:30.290 --> 00:09:35.610 even You can see everything exactly the same output by doing that. 148 00:09:35.610 --> 00:09:37.150 So that's using a union. 149 00:09:37.150 --> 00:09:41.730 Now the intersection of two sets includes members that are present in both sets. 150 00:09:41.730 --> 00:09:44.520 And again they only appear in the intersection set once. 151 00:09:44.520 --> 00:09:48.560 Now you can perform intersection using the and instead of calling the intersection 152 00:09:48.560 --> 00:09:51.420 method Well I wouldn't have used the and on sets part and 153 00:09:51.420 --> 00:09:55.400 just calls intersection, the in-built intersection function for you. 154 00:09:55.400 --> 00:09:58.640 So just to see how that would work, let's just close down the run window again. 155 00:10:00.010 --> 00:10:04.220 So let's just put a bit of a print here to put 156 00:10:04.220 --> 00:10:07.850 a bit of a gap between the other output, times 40. 157 00:10:07.850 --> 00:10:14.860 So if we do print Even .intersection 158 00:10:14.860 --> 00:10:18.520 squares, print. 159 00:10:18.520 --> 00:10:19.560 Again, we can do it this way, as well. 160 00:10:19.560 --> 00:10:22.770 We can, using the ampersand, squares. 161 00:10:24.200 --> 00:10:26.430 But again, using the ampersand internally, 162 00:10:26.430 --> 00:10:29.720 Python is just using the .intersection function anyway. 163 00:10:29.720 --> 00:10:31.944 And likewise, if you do a print squares. 164 00:10:31.944 --> 00:10:37.996 Thought intersection. 165 00:10:37.996 --> 00:10:43.097 Even print squares. 166 00:10:43.097 --> 00:10:47.390 So if you run that code. 167 00:10:47.390 --> 00:10:48.210 combination. 168 00:10:48.210 --> 00:10:49.540 We'll run that again. 169 00:10:52.020 --> 00:10:54.040 You can see we've got exactly the same output. 170 00:10:54.040 --> 00:10:56.760 These are the numbers that are present in both sets. 171 00:10:56.760 --> 00:11:02.280 And again looking up here and our original even set, the number 16 appeared there and 172 00:11:02.280 --> 00:11:05.350 in the second output number 16 appeared and likewise 4 and 4 and 6. 173 00:11:05.350 --> 00:11:13.320 And all the other numbers only appeared in 1 but not both of the original sets. 174 00:11:13.320 --> 00:11:15.270 So, bottom line here is using the ampersand. 175 00:11:15.270 --> 00:11:18.740 It really doesn't matter what you call the intersection method. 176 00:11:18.740 --> 00:11:20.050 You still get the same result either way. 177 00:11:20.050 --> 00:11:24.780 Now, sets can also be subtracted, and there's two ways to specify a subtraction. 178 00:11:24.780 --> 00:11:27.090 Which way you use is really just a matter of preference. 179 00:11:27.090 --> 00:11:29.950 Now, we'll cheat a little bit before we check that because all though Python's 180 00:11:29.950 --> 00:11:32.190 quite happily dealing with unordered lists, 181 00:11:32.190 --> 00:11:36.090 Us humans find them hard to read when of course there's more than a few members. 182 00:11:36.090 --> 00:11:39.940 So to make it easier to see what's going on, what we're gonna do is use the sorted 183 00:11:39.940 --> 00:11:43.070 function to sort the results before printing them out. 184 00:11:43.070 --> 00:11:45.270 Now this is not necessary when dealing with sets. 185 00:11:45.270 --> 00:11:46.670 I just wanna make that clear. 186 00:11:46.670 --> 00:11:49.350 We're only doing it here to make it easier for us to see 187 00:11:49.350 --> 00:11:53.170 what the various methods are actually doing with the actual sets themselves. 188 00:11:53.170 --> 00:11:58.430 So subtracting set b from set a removes any item that exists in 189 00:11:58.430 --> 00:12:00.990 set b from set a, if that makes sense. 190 00:12:00.990 --> 00:12:04.330 Just to say that again, so subtracting set b from set 191 00:12:04.330 --> 00:12:09.760 a removes any item that exists in set b from The first set said a. 192 00:12:09.760 --> 00:12:12.470 So with our example, so let's just comment this code out. 193 00:12:12.470 --> 00:12:15.380 And type some code in. 194 00:12:15.380 --> 00:12:18.630 And what I'll do is I'll just copy this first bit of code. 195 00:12:21.130 --> 00:12:23.070 We do need a little bit of that, and 196 00:12:23.070 --> 00:12:25.505 we'll just make a little space down the end here, so we can see what we're doing. 197 00:12:25.505 --> 00:12:29.605 So we'll un-comment that. 198 00:12:32.460 --> 00:12:34.160 So we're gonna have our even set again, but 199 00:12:34.160 --> 00:12:40.000 this time we're gonna do print sorted even to see what it looks like sorted. 200 00:12:40.000 --> 00:12:44.633 Then we'll leave our square tuple in again, and we'll put Squares = 201 00:12:44.633 --> 00:12:50.650 setsquares_tuple and then we'll also print. 202 00:12:55.164 --> 00:12:57.680 Source it squares. 203 00:12:57.680 --> 00:13:00.460 So, you've got a handle on what it looks like. 204 00:13:00.460 --> 00:13:02.790 All right. So, let's now do a, 205 00:13:02.790 --> 00:13:04.840 we'll put a heading here just to show what we're about to do. 206 00:13:04.840 --> 00:13:06.090 So, even. 207 00:13:06.090 --> 00:13:06.920 Money squares. 208 00:13:09.670 --> 00:13:12.060 So do that first, print, sorted. 209 00:13:13.180 --> 00:13:18.391 Even dot difference, since this is what's subtracted, is difference squares. 210 00:13:18.391 --> 00:13:24.515 So, and print sorted even takes squares, 211 00:13:24.515 --> 00:13:30.415 so the .difference and the minus, the subtract, does the same thing. 212 00:13:30.415 --> 00:13:36.445 And we'll try squares, cuz we'll do a heading first, so squares 213 00:13:39.814 --> 00:13:44.664 Minus even, so first, why don't we print 214 00:13:44.664 --> 00:13:51.384 squares.difference even, 215 00:13:51.384 --> 00:13:56.640 and print Squares tack even. 216 00:13:56.640 --> 00:14:00.410 The same deal here using the dot difference or 217 00:14:00.410 --> 00:14:04.460 using the subtract, the difference methods exactly the same as using minus. 218 00:14:04.460 --> 00:14:08.980 So which way you use depends on which you find more readable, in general probably 219 00:14:08.980 --> 00:14:11.900 the recommendation would be that the difference method is better because it 220 00:14:11.900 --> 00:14:15.150 makes it clear that you're working with sets which you may not know. 221 00:14:15.150 --> 00:14:16.530 If you're just looking at, say, line 62, 222 00:14:16.530 --> 00:14:18.600 you wouldn't know those variables are sets. 223 00:14:18.600 --> 00:14:20.070 Or if you saw a .difference, 224 00:14:20.070 --> 00:14:23.240 you'd know that that method is only applicable to sets. 225 00:14:23.240 --> 00:14:24.110 So let's run this. 226 00:14:25.400 --> 00:14:29.900 So what you can see in the first example is the subtracting Squares from even, 227 00:14:29.900 --> 00:14:34.258 removes the value 4, 16 and 26 from even, leaving only 9 and 25 and 228 00:14:34.258 --> 00:14:38.662 go the other way subtracting even from squares, remove 4,16 and 229 00:14:38.662 --> 00:14:41.492 36 from squares, again leaving only 9 and 230 00:14:41.492 --> 00:14:45.360 25 So I'm going to stop the video here cuz it's getting a little bit long. 231 00:14:45.360 --> 00:14:48.500 In the next video we'll continue our discussion, and we'll move forward and 232 00:14:48.500 --> 00:14:52.800 we'll talk about the update difference method as well, which is very similar 233 00:14:52.800 --> 00:14:57.650 to the difference method and the minus, that we've talked about just now. 234 00:14:57.650 --> 00:14:58.780 So, we'll see you in the next video.