Instructor: 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 gonna build the next game,
how am I gonna build the next Netflix,
how am I gonna 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 gonna 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 gonna 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 1, 2, 3, 4, 5.
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 5 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 5 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.add and then give it 2.
If I click run here, I see that I was able to add 100,
but 2, well, 2 wasn't really added
because my_set already contained that data.
It's unordered.
I mean, we've created 1, 2, 3, 4, 5 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 gonna be a fun little exercise.
If I gave you an array,
and let's say this array 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 wanna have duplicate emails.
We might wanna 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.
Going to change this into a set. There you go.
And then what if I wanted to access my_set at index of 0?
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 1 in my_set?"
And we run this, and we get True.
We can also do length of my_set and we get 5.
Again, nothing too crazy, but 1, 2, 3, 4, 5.
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.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
then 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.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 all set, which was my_set,
it's going to be empty, an 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.