Let's take a look at another data type called a set.
Now, a set is actually a lot less common than a tuple or a list,
and that has to do with the fact that they are unique,
and they also don't hold their order very well.
And holding order can be very important in a program.
So if I told you three things, three colors - red, green,
and blue, and then I asked you 10 minutes from now, what
were those three colors?
And you said - blue, green, red.
And I said, "Yes, but I was looking for a proper order".
Now, as an example of keeping data sort of in order, with
a list you could say operations of driving a car,
and this is what you have to do in this order.
You have to unlock the car, you have to get in the car, you
have to start the car, and then you have to drive away in your car.
And if I asked you if at any point in time I could drive
a car, the order of operations for a car is - I want to drive
away, then I want to get in the car,
then I want to start the car,
then I want to unlock the car,
that's not going to make any sense.
And that's why a lot of people don't use sets.
Also, the fact that a lot of people actually don't know that sets
even exist. Sets definitely have their place.
So I'm just going to comment this out because that's not
useful. And let's go ahead and create our first set.
And what do we want a set of?
Let's do a set of 'colors'.
So a set looks like this. It's a variable.
The variable name rather, is '=', and then opening and
closing curly brackets.
Now, this looks a lot like a dictionary, but a dictionary
has a key and a value, a set does not have that.
It simply has values.
So I could say '', I guess,
and that's fine. This is going to be a set.
And if I sort of display this, we have 'Blue'.
Here's a good example.
I said, 'Blue', 'Red', 'Yellow', 'Orange', 'Green'.
Python said, "I don't care about storing the order".
So 'Blue', 'Green', 'Orange', 'Red', 'Yellow'.
And these do not line up whatsoever.
So that's a good example of sets not keeping their order.
The other thing that makes a set very unique is the fact
that all the items have to be unique.
So if I change this to 'Green', so there's two Greens in here
and there's two 'Reds', and there's two 'Blues', and I rerun this,
and then I rerun this. Well, the order changed,
but these are all still unique.
There's still only ever 'Blue', 'Green', 'Orange', 'Yellow', and
'Red'. Now let's go ahead and add something to this. So we can do
'colors.', hit tab, and we can see all sorts of stuff.
Now in a list,
we appended to a list. Because a list is in order
we have to append to the back of it.
With a set, we don't have to append to the back of it because
it doesn't need to keep its order.
So we're just going to add to it.
And wherever Python says that data point gets stored, is where
it gets stored because we don't care.
We just care that the items are unique and that we have a
sequence that we can eventually loop through these.
So 'color.add', and let's add "Violet".
We'll add "Violet" in there.
And once again, sets are mutable, so you do not need to be
like 'colors = colors.add'
You can just do 'colors.add', and it will do it internally for us.
So if we type 'colors' again, we can see we've added, where
is "Violet"? Right there.
And I wonder, just for funsies if I display this out again,
I know it maintains its order this time, but let's see if
I add "Violet" again.
Nothing's going to happen because it's already unique, so nothing has changed.
Python was probably smart enough to not ever actually change
anything. But let's go ahead and add "Violet" once more.
So I'm going to copy that and put that here, and do 'colors'
I didn't add it.
If this was a list, "Violet" would be in there twice.
Now let's go ahead and remove something. So we can just do
'colors.', and then ;'difference', 'discard', 'intersection',
that is a very nice feature.
We won't get into that yet.
We can 'pop' just like in a list, but we also have 'remove'.
So let's 'remove('Orange')'.
Not that I have anything against Orange, but let's just get
rid of it. 'colors', and it is gone, just like that.
So it is a lot like a list in the sense that you can add
something, you can remove something.
It's just unique and does not maintain its order.
So if you have an order of operations that's important, and
most of the time, you probably will,
you'll want to use a list.
If that list never ever needs to change, you'll want to
use a tuple. If you have a bunch of data and it needs to be
unique, but it does not matter what the order is, and there
is a place in time for that,
then you want to use a set.
Now I'm going to show you a cool little trick here where
we can create a list of duplicate items.
So let's say 'Blue', 'Blue', 'Red', 'Red', 'Yellow',
And 'lst' is going to say 'Blue', 'Blue', 'Red', 'Red', 'Yellow'.
And we know that this is a list.
[no audio]
Oh, I skipped a thing that I've been doing as well.
So let's see what the 'type' is of 'color' or 'colors'.
This was a set. Python says, "Yep,
sure enough. That's a set".
Okay. So I want to make sure we covered that as well.
So we have this list '['Blue', 'Blue', 'Red', 'Red', 'Yellow']',
and let's say we don't care that the order is 'Blue', 'Blue',
'Red', 'Red', 'Yellow'.
We just care that these items are unique.
What we can do, is we can say 'colors =' and this
is called typecasting or variable casting in some languages.
And when you variable cast, you're going to take all these
items, and you're going to put it into a set.
So you're converting it from a list to a set.
So let's convert this list to a set, and we'll get into typecasting
down the road, but this is actually something really cool that I think is
very interesting and useful right away
as a new programmer. Let's go ahead and type 'colors'.
Now that has been cast as a set, we only have 'Blue', 'Red', 'Yellow'.
Again, we don't care about the order that it is.
It could be 'Yellow', 'Red', 'Blue',
It could be 'Yellow', 'Blue', 'Red'; 'Blue', 'Red', 'Yellow'; 'Blue', 'Yellow',
'Red', doesn't really matter.
And let's once again cast that.
So we changed this list, which is very clearly a list data type to a set.
Now, as an example, just to loop through all of these 'colors',
what we can do is, say 'for color in colors',
just simply 'print(color)'.
So it's going to loop through every item in the sequence.
It's going to say, "Hey, there's a Blue, there's Red, there's a Yellow".
But remember, it might not actually keep that order at all.
So let's go ahead and run that.
Sure enough, it didn't.
It went 'Yellow', 'Red', 'Blue'
And again, if that order of operations doesn't matter, and
you want that list to be unique, a set is a good way to go.