1 00:00:00,530 --> 00:00:01,620 - [Instructor] In this video, I'm going to 2 00:00:01,620 --> 00:00:05,390 focus on the mutable set operators and methods. 3 00:00:05,390 --> 00:00:09,690 And those are operators and methods you can use on 4 00:00:09,690 --> 00:00:13,110 a variable that refers to a set object to 5 00:00:13,110 --> 00:00:16,827 modify the set that the variable refers to. 6 00:00:16,827 --> 00:00:19,100 In this particular slide, we're 7 00:00:19,100 --> 00:00:22,490 looking at some of the mutable operations. 8 00:00:22,490 --> 00:00:26,290 First, we have the augmented assignments for the 9 00:00:26,290 --> 00:00:30,200 union operation, intersection operation, 10 00:00:30,200 --> 00:00:33,833 difference operation, and symmetric difference operation. 11 00:00:33,833 --> 00:00:35,970 In the bullets below each of those, 12 00:00:35,970 --> 00:00:38,610 we have the corresponding method names that 13 00:00:38,610 --> 00:00:41,150 can perform the same operations. 14 00:00:41,150 --> 00:00:44,470 Now, the key difference once again between, in this case, 15 00:00:44,470 --> 00:00:47,850 the assignments and the methods, is that the assignments 16 00:00:47,850 --> 00:00:51,840 have to have sets on both the left and the right sides of 17 00:00:51,840 --> 00:00:54,670 the operator and in the case of the left side of the 18 00:00:54,670 --> 00:00:58,260 operator, it has to be something that I can modify. 19 00:00:58,260 --> 00:00:59,630 So, it's typically going to be a 20 00:00:59,630 --> 00:01:03,530 variable that refers to an existing set object. 21 00:01:03,530 --> 00:01:07,360 And on the method case or in the method cases, 22 00:01:07,360 --> 00:01:10,530 each of these methods not only can receive as an argument, 23 00:01:10,530 --> 00:01:15,203 a set, but can receive any sequence of arguments and 24 00:01:16,250 --> 00:01:19,830 turn that into a set and then apply the operation. 25 00:01:19,830 --> 00:01:22,430 So, that's some of the operations 26 00:01:22,430 --> 00:01:24,250 we're going to look at in this video. 27 00:01:24,250 --> 00:01:26,320 We'll also look at various methods for 28 00:01:26,320 --> 00:01:30,310 adding and removing elements in sets. 29 00:01:30,310 --> 00:01:34,230 So, let's go switch over to a terminal window. 30 00:01:34,230 --> 00:01:37,410 And, to get us started, let me go ahead and 31 00:01:37,410 --> 00:01:41,610 create a numbers variable that refers to a set object, 32 00:01:41,610 --> 00:01:44,750 containing the unique values one, three, and five. 33 00:01:44,750 --> 00:01:47,880 And let's demonstrate one of those augmented assignments. 34 00:01:47,880 --> 00:01:51,400 Now, I'm not going to demonstrate all of them because they 35 00:01:51,400 --> 00:01:55,320 work the same basic way as the mathematical set operations 36 00:01:55,320 --> 00:01:59,650 we just looked at a few moments ago, two videos ago. 37 00:01:59,650 --> 00:02:02,340 And therefore, there's no need to show them all again. 38 00:02:02,340 --> 00:02:03,990 But, let's just show that you can, 39 00:02:03,990 --> 00:02:07,110 in fact, modify an existing set. 40 00:02:07,110 --> 00:02:11,090 So here we're going to do a union assignment operation. 41 00:02:11,090 --> 00:02:14,820 We're going to add all of the unique values from this set 42 00:02:14,820 --> 00:02:19,100 into the existing set, as long as they are not duplicates. 43 00:02:19,100 --> 00:02:22,830 And to confirm that, we can evaluate numbers and see that 44 00:02:22,830 --> 00:02:26,560 the duplicated three, of course, does not appear twice but 45 00:02:26,560 --> 00:02:30,270 all of the other unique values do indeed appear. 46 00:02:30,270 --> 00:02:32,132 Now, just as we can do that with the 47 00:02:32,132 --> 00:02:36,070 augmented assignment, we can also do that with the 48 00:02:36,070 --> 00:02:40,080 update method called in the case of a union operation. 49 00:02:40,080 --> 00:02:42,890 So let's do this, let's use a range call, 50 00:02:42,890 --> 00:02:46,550 which we know produces a sequence of integers, 51 00:02:46,550 --> 00:02:50,590 zero through nine, in this case because the argument is 10. 52 00:02:50,590 --> 00:02:52,120 And now what's going to happen is the 53 00:02:52,120 --> 00:02:55,670 update method will create a set out of the values from 54 00:02:55,670 --> 00:02:58,470 zero through nine, all of which happen to be unique. 55 00:02:58,470 --> 00:03:02,090 And it will then take all of the unique items from that set, 56 00:03:02,090 --> 00:03:04,326 combine them with the numbers set and 57 00:03:04,326 --> 00:03:08,320 remove any duplicates to produce the resulting set. 58 00:03:08,320 --> 00:03:12,040 So, if we go ahead and say numbers here, you can see we now 59 00:03:12,040 --> 00:03:15,980 have a set containing the values from zero through nine. 60 00:03:15,980 --> 00:03:19,700 So, again, all of the different augmented assignments and 61 00:03:19,700 --> 00:03:22,450 corresponding methods will work similarly to this, 62 00:03:22,450 --> 00:03:26,530 but of course performing their individual operations. 63 00:03:26,530 --> 00:03:30,040 Now, we also have the ability to add items that 64 00:03:30,040 --> 00:03:34,080 are not already in a set, using the add method. 65 00:03:34,080 --> 00:03:37,650 So, for example, if I say numbers dot add and 66 00:03:37,650 --> 00:03:41,653 I give it as an argument, 17, which is clearly not in 67 00:03:41,653 --> 00:03:45,620 this set yet, if I evaluate numbers after doing that, 68 00:03:45,620 --> 00:03:49,120 we can see indeed 17 was added. 69 00:03:49,120 --> 00:03:52,150 Of course, if I say numbers dot add and I give 70 00:03:52,150 --> 00:03:55,130 it a value like three that's already in there, 71 00:03:55,130 --> 00:03:58,410 whoops I spelled numbers wrong, so let's do that again, 72 00:03:58,410 --> 00:04:01,350 numbers dot add and give it a three, 73 00:04:01,350 --> 00:04:04,753 and then I evaluate numbers again, you can see that the 74 00:04:04,753 --> 00:04:09,753 set has not changed from back up here in snippet seven. 75 00:04:09,770 --> 00:04:13,430 Now, sometimes you may wanna remove an item from a set and 76 00:04:13,430 --> 00:04:16,820 it turns out there's several different ways to do that. 77 00:04:16,820 --> 00:04:20,750 So, let's say we would like to remove the value three. 78 00:04:20,750 --> 00:04:25,310 So, if I go ahead and say numbers dot remove with three as 79 00:04:25,310 --> 00:04:29,540 an argument, now three is in the set at the moment so 80 00:04:29,540 --> 00:04:33,750 this will work, if in fact the value was not in the set, 81 00:04:33,750 --> 00:04:37,280 I'd actually get a key error as a result of that. 82 00:04:37,280 --> 00:04:39,120 So you can try that on your own. 83 00:04:39,120 --> 00:04:42,270 But, let's go and evaluate numbers and now we can see that 84 00:04:42,270 --> 00:04:45,760 the value three is no longer in the set. 85 00:04:45,760 --> 00:04:49,420 So you have the remove method to remove an item and 86 00:04:49,420 --> 00:04:51,270 if the item is not there, you'll get 87 00:04:51,270 --> 00:04:53,230 an exception called a key error. 88 00:04:53,230 --> 00:04:55,900 If you simply want to get rid of an item and 89 00:04:55,900 --> 00:04:58,660 you don't care whether it was there or not, you can 90 00:04:58,660 --> 00:05:03,170 actually use a method named discard instead of remove. 91 00:05:03,170 --> 00:05:05,980 Discard does exactly the same thing but does not 92 00:05:05,980 --> 00:05:10,230 cause an exception if the value is not in the set. 93 00:05:10,230 --> 00:05:13,790 Now, if you don't care which value you remove, 94 00:05:13,790 --> 00:05:15,740 you just want to remove a value, 95 00:05:15,740 --> 00:05:18,090 perhaps you're working your way through the set and 96 00:05:18,090 --> 00:05:20,260 you just wanna grab one value at a time and 97 00:05:20,260 --> 00:05:22,240 do something with it, you can use 98 00:05:22,240 --> 00:05:25,220 the pop method for that purpose. 99 00:05:25,220 --> 00:05:28,930 And because sets are unordered, you should not rely on 100 00:05:28,930 --> 00:05:32,935 what value will come out next each time you call pop. 101 00:05:32,935 --> 00:05:36,160 In this case the value zero was returned. 102 00:05:36,160 --> 00:05:38,540 And if I now go and evaluate numbers, 103 00:05:38,540 --> 00:05:42,070 I can see that zero is no longer in the set. 104 00:05:42,070 --> 00:05:45,860 And finally, if you ever needed to completely clear out a 105 00:05:45,860 --> 00:05:49,240 set, you, of course, could use the clear method as we 106 00:05:49,240 --> 00:05:53,350 have on other of the built in collections in Python. 107 00:05:53,350 --> 00:05:55,580 And if I evaluate numbers after that, 108 00:05:55,580 --> 00:05:59,050 I can see that I get the empty set representation. 109 00:05:59,050 --> 00:06:01,640 And again, because curly braces with 110 00:06:01,640 --> 00:06:04,870 nothing in them represent an empty dictionary for 111 00:06:04,870 --> 00:06:07,210 an empty set, they display that as the 112 00:06:07,210 --> 00:06:10,823 word set followed by an empty set of parentheses.