 Welcome back everyone. My name is Brian. And in this video, we're going to talk about the set. Now, the set is a little bit different than the list. We spent 30 plus minutes in the last video of deep diving into the list. If you haven't watched that video, I highly encourage you to go back and watch it because we really do dive into the list. Set, however, is slightly different. That difference though means it is profoundly different under the hood. Let's take a look at this. So a set contains unordered, unique and immutable data types in a hash table. Let's break that down. What does that really mean? Unordered, meaning we cannot control the order like in a list you could add or insert meaning we can now control the order can't do that with a set. The set determines the order unique meaning you cannot have multiple. So if you try to add two cats or two dogs, it's just not going to do it. And immutable data types. What does that mean? Means that once we add them, we cannot change them, we can only remove them or add them, we cannot change individual items. And it's in a hash table. Hash table, well, that gets a little bit complex in a computer theory, but really, when you hear the term hash table, think blistering fast read access. The set determines the order so that it knows exactly where things are in memory. And it makes it very fast. So if you want very, very quick lookups, a set is what you need. Now that we understand what a set is, let's go ahead and let's create one. So first things first, we'll say s equals and then we want these little curly brackets. Remember, if you do the square brackets, that's a list, completely different data type, you want the curly brackets. And in here, I'm going to just say one, one, two, come to come to come three, four, and five, you may be going now, wait a minute, I thought it had to be unique. Well, this is one thing I absolutely love about Python, it automatically strips all that out for you. So you don't have to worry about it. See 12345, even though we tried adding multiple twos in here. It's smart enough to realize it already exists and says no, can't do it. And it doesn't raise an error or anything like that. So we don't have to worry about it exploding on us here. We can also, in case you're wondering, convert a list into a set. And let's show how to do that. I'm gonna say, here's our list. And I'm just going to have my name and my age. And now we're going to say s equals and we're going to use the set function. And what this does is take any sort of editable data type like the list, which we've talked about, and convert that does it automatically, we don't even have to think about it. I'm going to grab this. Now notice the order. I did Brian Karen's 46. And in a list, it's going to be exactly the way we put it. However, in the set, it's actually 46 Brian Karen's. So the set is now determining the position. That's one thing you really, really drive into your brain about a set is the set is unordered, unique. And as we're going to see later on immutable data types. Okay, let's see how we would add items to a set. It's actually very, very simple. So I'm gonna say s dot add. And you can add pretty much any element type you want. I'm just going to add the word Hello. There is another way though, and it's called update. And it looks a little bit challenging when we look at this, it says, star s colon iterable t none, what does all this mean? When you see this term right here, basically, we're talking about something like a list, I shouldn't say a list, but like a list, something that you can go through or navigate through like a list or a set or a tuple, which we haven't talked about yet. You just need some sort of data container that contains multiple items that you can iterate through. So in this case, we're going to say, we want to do this in a list. Say 123. And I'm going to go ahead and add in Hello. Notice I've got Hello twice, but we're working with a set which is unique items. So what's going to happen under the hood? If you're going to guess that the set is going to automatically strip out the second Hello, you'd be absolutely correct. Let's see it in action. 12346 Karen's Hello, Brian again, unordered and unique items only. Now that we've added items, let's look at how we would go ahead and remove items. And it's just as simple as discard. And discard does well exactly what you think it would, it just takes it and throws it right in the trash. However, there's a subtle difference between discard and remove. Pop quiz. Does anybody know what the difference really is here? Well, one will throw an error and one will not. So discard will not throw an error, where remove will throw an error. And what do we mean by that? If we run it in its current form, everything is well just going to work. Let's go ahead and modify this a little bit and demonstrate this error. So in our set, we have 123, Karen's 46, Brian and Hello. Let's go ahead and say some number that we know is not in there 78. Run this and oh, we have a key error 78, which means it knows 78 is not in there. In future videos, we're going to talk about how to get around that you're going to use the keyword in to determine whether or not it's actually in there. But just for our beginner's perspective, discard will not throw an error and remove will be very mindful of that as you're moving forward. Another thing we can really do here is we can pop now pop for a set really isn't abundantly used. I'm just going to say it. You may be inclined to say, Hey, I want to get like the first item or the second item or the last that you can't do that, it's just going to return an arbitrary item, you don't control what item it gives you, it's just going to give you an item. That gets a little frustrating. But if you're just using the set as a container, and you want to just get each item and remove it pop is actually pretty useful. But this is going to be pretty random. As far as you're concerned, and you're going to have zero control over it. Now that you've popped it, you could do whatever you want it with V because it still exists in memory. However, it's no longer in our set. One question I'm often asked, especially by newbies is, Well, I have a set and I want to modify it. Well, unfortunately, you can't. Not only can you not modify it, you can't access items based on their index because the set has no concept of this. Let's go ahead and demonstrate. So I'm gonna say s and zero. And we're going to change this to a, go ahead and run this see what happens. And object does not support item assignment. Oh, that is super frustrating. This is actually by design. Once you put it in the set, it will not change because under the hood, it's a hash table. It's making a mathematical computation to determine where in memory it needs to go so we can do a fast lookup. Okay, not a huge super deal. But I just want to print one item out. Let's just try to print that first item out and see what happens here. Uh oh. Set object does not support indexing. Oh my goodness, that is so frustrating. You may be thinking, well, this is stupid, why even use this? Let's just use a list and get it over with. Well, you can. However, a set is designed for specific reason that is fast lookup. Think of a set like you would have settings, like for example, all of your computer settings, there's probably thousands and thousands of computer settings. If you put that in a list, it's going to be hard to really navigate. So you would use something like a loop or use the keyword in things that we haven't covered yet, and we will cover in future videos. But what we're going to talk about is some down and dirty ways you can modify a set. So for example, let's say, hmm, let's run our code here and see what we got. So we've got 346 Karens and Brian, let's go ahead and say three in s, go ahead and print this out just to see this is in there. True. So we know that three is in s, let's go ahead and say s that remove. And we want to remove that three. Now, we want to go ahead and turn around and add. Well, so basically what we're doing is we're saying take the set, remove this three and change it to a 12. Unfortunately, we cannot control where it's going to put it in the set. Let's go ahead and print that out probably a little more helpful. There we go. So now it's 1246 Karens of Brian. Honestly, dumb luck that it's in the same position. Don't count on that because the set is unordered. Now we can do some crazy things here and this is going to really hurt your brain. So let's go x equals and we're going to make a set. And let's say a b c d. And I'm quite literally just freestyling this. So expect me to make some typing errors here. And we're going to say C E F G. So we've got two sets. Now we want to do some mathematical computations on these. And this is kind of the standard way of doing any sort of set modification. We're going to do what's called a union. So I'm going to say s equals and we want x union y. And what this is going to do is it's going to and I'm going to see if I got some notes off the screen here. All the elements that are in either set. And I'm going to do a little print statement here. So our union is now all the elements that are in either set. Very rapid modification of that set. You don't have to go in, remove and add, remove and add and check and see if it's in there. And just grab this whole thing. And let's go ahead and do an intersection. Now an intersection is a little bit different. It's going to get all the elements that are in both sets. But demonstrate that. And what's in both is D and C. Pretty simple, pretty easy to wrap your head around. Now let's get a little bit crazy here. So we're going to grab this. And we want the difference. You ever have somebody say that to you, let's, you know, split the difference. So the difference are all the elements that are in X but not in Y, which in this case is B and A. And one more that we're going to cover here. And this is the maybe if I get my mouse to work, the symmetric difference. Now this is going to be a little bit different than what you're thinking. So this is all the elements that are in one of the sets. What does that mean exactly? Save and run, all of the elements that are in one of the sets. So it's going to take both of them and basically make a third set in memory and say, okay, make sure what we're giving back is a unique order of items that exists in at least one of the sets that we've given it. And it gets way more complex than that. But that's basically what's going on under the hood. You can see we have F, E, B, A, G. So the main takeaway from here is all the way up at the top sets contain unordered unique immutable data types in a blistering fast hash table. What is the fundamental difference between a set and a list? Well, the set is well, a little bit more challenging to work with and you cannot modify it as easily as you can with a list, but it is much, much faster, which you're really not going to understand how fast that is until you get into more advanced programming. Right now we're learning the fundamental data types, and we just covered the set in depth. I hope you enjoyed this video, you can find the source code out on github.com. If you need additional help, myself and thousands of other developers are hanging out in the void realms Facebook group. This is a large group with lots of developers and we talk about everything technology related not just the technology that you just watched. And if you want official training, I do develop courses out on udemy.com. This is official classroom style training. If you go out there and the course you're looking for is just simply not there, drop me a note, I'm either working on it or I will actually develop it. I will put a link down below for all three of those and as always, help me help you smash that like and subscribe button. The more popular these videos become the more I'll create and publish out on YouTube. Thank you for watching.