Can you believe it?
I hope I didn't put you to sleep, but we're almost there.
We have one last main data type set, and then we're done.
I know, I know this isn't the most exciting topic.
Trust me, this is the hardest part.
When you're starting out and you're just learning these things without understanding.
Why do I need this?
How am I going to build the next game?
How am I going to build the next Netflix?
How am I going to build the next coolest Apple app?
You're looking at this and thinking, I just learned a bunch of theory.
I feel like I'm in math class all over again.
I know.
Trust me, I went through this.
I was a self-taught programmer as well.
These are the Lego blocks that builds our foundation.
Once we learn these, we can move on to new things and you'll see that once you climb this little mountain,
things become so much easier and you'll realize the power.
When we start building these projects toward the end of the class where you're going to say, Oh, all
these blocks that we learned, all this frankly boring part is all going to make sense.
So hang in there.
It's supposed to be hard.
It's supposed to be tough, but you'll see very soon why this is so powerful and why what we've just
learned is going to allow us to build some really awesome things and create some really awesome programs.
So let's finish off with the last one, the last data type, the last data structure that we'll see
in Python, at least from the main ones.
And it's called set and sets are simply unordered collections of unique objects.
Hmm.
Let's have a look.
Let's create a my set and my set.
We create with curly brackets like a dictionary.
But instead of a dictionary, we don't have key value pairs.
Instead, we just have values like one, two, three, four, five.
That's it.
That's a set.
We just created a set.
But remember, the definition I gave you set is an unordered collection of unique objects.
What does that mean?
Well, if I print this and say my set and I click Run.
I get a set.
But if I had, let's say another five in here and I click Run.
It only returns the unique sets or unique items you see in a set.
There's no duplicates.
Everything has to be unique.
So this five just never gets added to a set.
For example, you can add things to a set like my set, and then I can say add and give it a value like
100.
And then let's say I do my set dot ad and then give it to if I click run here.
I see that I was able to add 100, but two well, two wasn't really added because my set already contained
that data.
It's an ordered.
I mean, we've created one, two, three, four, five here, but there's no real order to it.
There's no bookshelf in our memory space that is right next to each other.
These are all over the place in memory, but a set is able to find them because they're all unique.
So they're all in just one location in memory.
So let me ask you this.
And this is going to be a fun little exercise.
If I gave you an array.
And let's say this are contained.
Well, this right here.
And I want you to return an array with no duplicates.
So I want everything in.
Let's say.
Let's call it my list.
And a programmer has already created this.
Okay.
And then your task is to create or return a list or a collection that has only unique items.
How would you go about converting this into a set?
Well, we've seen this before.
We have a set function and we simply wrap my list in that set.
And let's print it out.
I click run.
Check it out.
We have formed a new set from a list.
And I've removed all duplicate values.
When would this be useful?
Imagine if we had usernames, right?
Or email addresses.
We're collecting email addresses on our startup page, but we don't want to have duplicate emails.
We might want to convert this a list of emails to a set and remove any duplicates.
So we're not sending emails over and over.
So that's really cool.
What about this?
Can I.
Let's change this into a set.
So let's call it my set.
It's going to change this into a set.
There you go.
And then what if I wanted to access my set at index of zero?
Hmm.
Set object does not support indexing.
Well, again, you can think of it more as a dictionary.
In order to access a set, we have to grab by the item that's in it.
That's the key to getting the information from memory.
The way we would check if something exists is we simply say, Hey, is one in my set and we run this.
And we get true.
We can also do length of my set and we get five.
Again, nothing too crazy but one, two, three, four, five.
Remember it only counts the unique things because this will never gets entered.
It's a set.
And alternatively, we can also convert this into a list.
If I click Run here, you'll see that I now have my set as a list.
And then we can also do, let's say my set dot copy and actually copy my set into something, let's
say new.
Or new set, and then this will be completely different or a new copy than this original one.
So let's test this out.
This new set.
If I use a method that we may have seen before, which is my set dot clear, which as you can imagine,
clears the set and I click run.
I have that new set.
But when I try and print our old set, which was my set, it's going to be empty and empty set.
But the true power of sets comes in the next couple of methods that I'm going to show you.
And sets have quite a few methods, but don't worry, most of them are quite similar.
So let's explore that in the next video.