You've previously learned about lists and tuples. In this video, we're going to talk about yet another sequence type in Python called a set. Now, a set is similar to list and tuples. However, sets do not have a specified order, nor can they contain duplicates. So if you have a collection of data where you don't care what order it's stored in and you don't need to keep duplicate, sets can be a more efficient structure than lists or tuples. So, let's take a look at how sets work. First, let's take a look at how we can create sets in our Python programs. Well, just like other sequences like lists and tuples, we can type sets directly into our program. We simply enclose a sequence of items with curly braces. Unlike lists where we use square braces or tuples were used parentheses, sets are indicated by curly braces. So you see here, I have a set of numbers, where I typed in four numbers, I have a set of letters with five letters in it, and I have an empty set that has no elements in it. Let's run this and see what happens. Well, when we look at my numbers set, things look a little bit different than when I typed that in their program. This is the first thing that you need to understand about sets. There is no order. Python is free to store the elements in a set in any order that it chooses, and you cannot rely on this. So, while I typed the numbers in 3, 2, 1, 4, Python chose to store them in the order of 1, 2, 3, 4. Let's look at our letters. That's the second thing that you need to understand about sets. Notice that I had five characters in the set that I typed in, yet there are only three characters in the set that Python printed out. Why? Well, you cannot store duplicate in sets. I had two letter As and two letter Bs. And Python does not allow this, so it condensed those to a single A, a single B, and then a single C. So if you try to add a duplicate element to a set, it won't happen. You'll just have the single element that was already there. And then finally, let's take a look at our empty set here. Well hopefully, you realized there was something wrong here. Empty curly braces is actually an empty dictionary in Python. Python has its peculiarity where curly braces can indicate either a dictionary or a set. If the contents between those curly braces are key value pairs indicated by a key, then a colon, then the value, all separated by commas, then that isn't dictionary. If the elements inside the curly braces are simply elements separated by commas, then that indicates a set. If you have no elements inside the curly braces, Python says that's a dictionary. So you can see this printed out empty curly braces, and then says, "This is of type dict." So how do we then get an empty set? Well, there is also a function called set in Python that will allow us to do this. If I call set with no arguments, that will create an empty set. You can see now that Python printed out set with no arguments, and says, "This is of type set." You can also use this function to convert other things into sets. So, if I have a list and I would like to create a set out of it, I simply call the set function with my list and a set will be created. And you can see here, I have a list that does have duplicate elements. There are multiple number ones and multiple number threes in my list. I call set. Those go away in my set, so I only have the elements 1, 3, 5, and 6. You can also pass any iterable object to the search function. So for example, I can call range and pass that to the set function. We've previously seen how you can convert the result of calling range into a list by passing it to a list function. Similarly, you can pass it to the set function. And you see here, we end up with a set that includes the numbers zero up to, but not including, five. Once we have a set, we can add and remove elements from that set. So let's take a look at how we might go about doing that. There is a method on sets called add. So, I go set1.add10, which should add the element 10 to that set. There is a method called pop, which removes an arbitrary element from the set. Since sets are unordered, I have no idea which element is first or last. Pop simply selects some element within the set and returns it. And that element is removed from the set. I can also call discard if I would like to discard a particular element from the set. So you can see here that I asked for the element three to be discarded from the set. Discard is interesting in that if the element is not in the set, it does nothing. If it is in the set, it takes it out. Let's see how these work. You can see that set1 now contains, not only 1, 3, 5, 6, but also the element 10 because I've added it. I call set1.pop. It decides to give me back the element one. And we print out the set again. You can see one has been removed and now we only have 3, 5, 6, and 10. And then, I can explicitly discard the element three, leaving us with only 5, 6, and 10. Finally, if you'd like to access all of the elements of a set, you can iterate over the set. And the iteration syntax should look familiar to you. It's basically the same as if we were iterating over a list or a tuple. I can do four item in letters, and then item takes on each element of the set in turn. Because sets are unordered though, I have no way of knowing which order I'm going to get the items from the sets. So, you have to be prepared that you can get them in any order. Let's see what happens here. We print out the items A, B, C. Now, you can't rely on that always being in alphabetical order. It just happens to be in this case. Now you have another tool in your Python programming toolbox, that of sets. Sets can be better than lists if you know that you do need to store duplicates and you do not care about the order that the elements are stored in. If those are true, Python uses these constraints in order to store the set more efficiently so that it is much cheaper to check if an element is in a set. It's also very cheap to pop an element out of the set because it can pick any element that it would like. Adding an element is similarly cheap. So, sets can be much faster than lists, although, you're probably not going to see this benefit unless you're storing very large amounts of data. But nonetheless, the advantage is there. So, if you have data in which you don't care about the order and you don't need to store a duplicate, sets are a good choice. There are also a lot of other methods that you can perform to operate on sets, and I encourage you to look through the documentation if you're interested.