[MUSIC] Okay, we're getting down to the end here. We have half of a game but we really want to build the other half, right? In order to make your game really interesting, we need to be able to add a bunch of items. You're going to need a collection of rocks and a collection of missiles. And we need to be able to have them interact so that you can tell when things hit each other. Okay, so how do we do that? Well, one of the key things here is we need a way to keep track of all those objects. So in today's lecture, we're going to talk about sets in Python. And sets are a way that we can keep track of collections of data. So with that, let's get started. So far we've seen several data types in Python that allow us to store a bunch of items. And the first one that we've seen that we've been using quite a bit is a list. A list is an ordered, Sequence of data, okay? The second one that we saw is a dictionary. And a dictionary is a key to value mapping. In this video, I want to talk about a set. What is a set? A set is an unordered, Collection of data, With no duplicates. All right and each of these types has their appropriate usage, okay? If I need to keep things in order, I need to use a list. If I want to map keys to values, I need to use a dictionary. So when do I use a set? Well a set is kind of like a list. I can put a bunch of stuff in a set. But the fact that it's un-ordered and doesn't allow duplicates makes it faster to use than a list. So if I don't care about ordering and if I'm not going to have duplicates then I should use a set. So what's an example of a set? So we've seen a list in Python looks like this, [1, 2, 3, 1, 2], there's a list with five elements. Well a set in Python looks like this, [1, 2, 3], all right. And I didn't add that extra one two because I could not have that in the set because a set does not allow duplicates. All right? This is a powerful construct that allows me to more efficiently test is something in the set, to add something to the set, or remove from the set, than I could do with a list. Okay, rather than continuing to talk about sets in the abstract, let's just use them. Let's create a set of instructors for this class. All right, there we go. You can see that the way to create a set is to first create a list. All right, so I've got a four element list here and then pass it to the set function. That will create a set. Let's run this program and out comes a set. Python uses that exact syntax to print it and set with parentheses around the list. Now, you'll notice it didn't come out in the same order that it went in. Python decided Warren should go first, I'm not really sure why, obviously I should be first here, okay. That's because sets are unordered. Python is free to make whatever choice it wants about the order of the set, and this is part of the reason why things actually get faster when you use a set over a list because it choses the specific order that allows it to be faster. All right, so it doesn't matter what order you put it in here. You may or may not get the same order out. But that's irrelevant because you implicitly do not care about duplicates. Now, what if I have a second list of, set of instructors here? I really think that I should be teaching a lot more. Maybe you're a little bit crazy, and you think Warren should be teaching a lot more, and we still want Greiner and Wong. Okay, let's print instructor two. Let's make that a little bigger so you can see it. All right, so I have exactly the same set. Even though I tried to duplicate Rixner and Warren and that's allowed inside the list here. When I call the set function, duplicates get eliminated. So you will only see one of each thing at the end. All right, let's actually check, are these two sets equal to each other? I can compare equal or not equal to, and the answer is true because they have exactly the same elements in them so they are equal. All right, so that's one thing I can do with sets. I can also iterate over sets. Okay, I'm going to print out each instructor and we see we get Warren, Rixner, Greiner, Wong. Now you'll notice that is not the order that I put them in here. So you are not guaranteed any particular order when you iterate over a set. What you are guaranteed is that you will see each item once and only once. Okay, so you're guaranteed to see everything, you just are not guaranteed what order you see them. All right, so what else can I do? Well, it's been a long semester, eight weeks is a long time. I'd like to see some new instructors here. It's getting a little bit boring, so let's find somebody a little bit more exciting. So add Stephen Colbert to our roster here. So what happens? Right, you can see I've now added Colbert to my set. Can I add something that's already there? I'm guessing that you already know the answer. I really want to get in there second time. But no, it will not let you. So if you try to add something that's already there, it just doesn't add it because it's already there. Now, I can also remove from sets. Who is this Wong guy anyway, never heard of him. Let's get rid of him. All right, do that. You can see we're now down to our previous four instructors except Wong is replaced by Colbert. What happens if I try to remove somebody who's not there. So Wong is already gone, let's remove him again. Here, okay so we're going to get an error if we try to remove something that has already been removed from the set. So that could be a problem, how do I deal with this? If I don't know if something is in the set how do I remove it? Well, conveniently there is an in operator, That return true or false Okay, and so if I run this, you see it returns true, Rixter is in the instructors, false, Wong it is not in instructors. Okay, so these are the basic operations of sets. I can create a set using the set function, I can compare sets for equality with ==, I can iterate over sets using four loops just like they were lists, except I'm not guaranteed what order I'm going to get elements out. I can add instructors, I can remove instructors, and I can test if instructors are in the set. Okay, so let's put sets into the context of our game. Our game is going to have collections of rocks, and collections of missiles. And it's convenient to keep them in a set. Why? Well, we don't care about their order, and there's not gong to be any duplicates. So I can keep them in a set. I can run, I can iterate through the set to update them, and update their position. I can iterate through the set to draw them. And I can iterate through the sets to figure out if I've collided with them, okay. So let's start back here with our sets of instructors. And let's talk about working with sets. And I'm not going to give you an example that actually shows you how to collide with things. Rather, I'm going to show you an example of how to do something to a set. So I want to create a function, so I've got my instructors here. Right, and they still print, everything is as it was before. I want to create a function and I'm going to call it get_rid_of, and I have an instructor set and a starting_letter. So I want to write a function that get rid of all the instructors whose namea start with the starting letter. So first I'm going to create a new set, let's call it remove_set and this is going to start as an empty set. I'm not going to remove anything unless I find somebody with that starting letter. Okay, so now I say for instructor in instructor set. What do I check here? Well, if instructor 0, the first letter of their name is equal to the starting letter, what do I want to do? Well, then I want to add them to the remove set. Why don't I just remove them directly from instructors set right now or instead? Well, because it's a bad idea to be modifying the thing that you're iterating over. And we talked about this with lists. If you do that, it leads to errors. You shouldn't be doing it with sets either, okay. So we remove_set, sometimes with sets it works, but you should be safe and not do it, okay? So when I'm done with this loop, now remove_set is the set of instructors I want to get rid of from the instructor set. Now there's the set function called difference_update. Okay, and I'm going to let you read the documentation but you can guess what it does based on what happens here. So let's call get_rid_of (instructors), and people starting with a 'W' and now let's print instructors, okay? Oops, that's an error, I wanted to add to that set, not just call it. Now I run it, and all the instructors whose names begin with W are magically gone from the set. Right, it's not actually magic. I've iterated through, I've created a remove set here that includes all of the people that I want to remove. Difference update, like I said, you should read the documentation. But hopefully it's obvious that it takes this set and gets rid of all the elements in that set from the instructor set. So let's get rid of people whose names that begins with g. There you can see that Greiner disappears. Let's try a lowercase w. You can see that nobody is removed. So this is a way of operating on sets. I'm going to leave it to you to think about how you can use this kind of structure to check for collisions and perhaps remove items from the set, or create explosions, or the like. All right? So hopefully now you have a basic understanding of the concept of sets in Python. And you can see how we might use sets to keep track of a collection of objects in your game, okay. In the next video, we're going to talk about how you might keep track of collisions in your game. So if we add these two things together, you're pretty close to being dangerous.