1 00:00:00,663 --> 00:00:04,186 Now that we know different ways to make sets, 2 00:00:04,186 --> 00:00:07,622 let's learn about how to manipulate them. 3 00:00:07,622 --> 00:00:11,798 We'll start with adding and removing elements. 4 00:00:11,798 --> 00:00:15,861 Remember that sets are mutable, that means, 5 00:00:15,861 --> 00:00:21,809 that the data inside of a set can be changed after the set is created. 6 00:00:21,809 --> 00:00:26,023 There are two methods for adding elements to a set. 7 00:00:26,023 --> 00:00:29,467 If an element is already a member, there is no effect. 8 00:00:29,467 --> 00:00:34,638 We can add one element at a time with the add method, and 9 00:00:34,638 --> 00:00:40,271 we can add many elements all at once with the update method. 10 00:00:40,271 --> 00:00:46,136 Let's demonstrate! First, I'll declare a new variable, 11 00:00:46,136 --> 00:00:50,776 let's have vowels and assign it as an empty set. 12 00:00:53,190 --> 00:00:58,031 Then, we can use the add method to add one element, 13 00:01:00,070 --> 00:01:06,223 And I'll add in, A, which was the first vowel, we'll do a capital A. 14 00:01:09,463 --> 00:01:14,263 So if I print my vowels out, and 15 00:01:14,263 --> 00:01:18,875 I run my script in the console 16 00:01:23,201 --> 00:01:25,070 I'll see that I have a set. 17 00:01:25,070 --> 00:01:32,219 It's a singleton set with one member and the member is a capital A. 18 00:01:32,219 --> 00:01:38,070 This method has no effect if an element is already a member of the set. 19 00:01:38,070 --> 00:01:43,218 We'll make a simple check for membership using the "in" keyword. 20 00:01:48,851 --> 00:01:57,455 And what I'll do here is I will print out... 21 00:02:08,630 --> 00:02:16,258 We see it's true that A is a member of this set of vowels. 22 00:02:16,258 --> 00:02:21,923 Now what will happen if I duplicate and 23 00:02:21,923 --> 00:02:26,708 add in this A many, many times. 24 00:02:31,684 --> 00:02:37,685 When I run my script again, we'll see that I still have a singleton 25 00:02:37,685 --> 00:02:44,125 set! Because A is already a member of vowels, it will not be added again. 26 00:02:46,395 --> 00:02:50,768 Now, instead of adding in the rest of the vowels one by one, 27 00:02:50,768 --> 00:02:53,054 let's add them all together. 28 00:02:53,054 --> 00:02:58,550 I can use the update method to add multiple values. 29 00:02:58,550 --> 00:03:04,075 It takes an iterable as an argument, just like the set constructor function. 30 00:03:09,888 --> 00:03:14,698 I'll use my favorite iterable type, which is a string 31 00:03:16,741 --> 00:03:21,493 E, I, O, and U. Just like the set constructor function, 32 00:03:21,493 --> 00:03:27,136 the update function will go through every element of the iterable, 33 00:03:27,136 --> 00:03:29,517 and then add it into the set. 34 00:03:29,517 --> 00:03:34,882 Let's print out my new vowel set after it has been updated. 35 00:03:40,325 --> 00:03:45,345 We'll see all of the vowels have been added into the set. 36 00:03:49,604 --> 00:03:54,603 When elements are already a member of the set, the method will have no effect. 37 00:03:59,045 --> 00:04:06,448 We can see that even though I've tried to add the letter A many times, 38 00:04:06,448 --> 00:04:11,424 and then the letters E, I, O and U many times, 39 00:04:11,424 --> 00:04:16,167 they're only added into the set just once. 40 00:04:16,167 --> 00:04:20,286 So, there's our set of vowels. 41 00:04:20,286 --> 00:04:26,033 Notice how the order is non-deterministic for these string data types. 42 00:04:26,033 --> 00:04:30,042 So if your vowels come out in a different order, 43 00:04:30,042 --> 00:04:34,461 that's totally okay, it's working as intended. 44 00:04:34,461 --> 00:04:40,224 Add and update are the two methods for adding elements into a set. 45 00:04:40,224 --> 00:04:43,560 Next we'll learn about removing elements from a set.