Tuples and Sets are a lot like Lists which we learned about in our last video. They both store multiple data. However, there are some big differences. Sets cannot have any duplicate values and a set is also unordered (no index). Tuples are like lists that cannot be changed (immutable). These both offer some advantages. We can take advantage of the fact that sets don't allow duplicates to remove duplicates from a list with ease by simply converting a list to a set. Sets also allow us to identify in Venn diagram fashion, the elements in common between sets or the differences, etc using union(), intersection(), difference(), and symmetric_difference() methods. We do an example where we pull the keywords from the internet movie database for Fellowship of the Ring, Harry Potter (Sorcerer's stone), and Star Wars (A New Hope) and see what they have in common etc. Try it yourself with the .csv file available on my github. https://github.com/sp8rks/MSE2001python/blob/main/module_examples/data_for_exercises/fantasy_sets.csv Tuples make assignments simultaneously so we can use these to easily swap variable values. We can also take advantage of packing and unpacking with tuples.
In today's video, we're going to talk about two things. We're going to talk about sets, and we're going to talk about tuples.
So these are really similar to the lists that we learned about in a previous video, but there are some key differences.
So for one thing, what do we know about a set? We said earlier that a set is a collection which is both
unordered and unindexed, and it has no duplicate members. So a list definitely is indexed, right?
We talked about this whole funky way that Python indexes things.
We don't have indices with sets, right? And you cannot have duplicate members. That's the other big difference.
So let's try and go ahead and do this. We're going to create our first set.
So we'll just call it X, right?
So the set X is going to be equal to, we're going to use curly brackets now
instead of square brackets for a list, and we can put whatever we want in here.
So let's put, they have to be something that's immutable though, right? So it can't be changing.
So let's do like a cat. We can do words in here, strings. We can do dog.
We can do fish, right? So we've got those three things.
We go ahead and run this, and it's going to create a set, right?
It has three objects in it, or it has three, it has a size of three,
and it has cat, dog, and fish inside of it. If we wanted to create an empty set, you could not do this.
Let's say we wanted to create
the variable empty set. You can't just use curly brackets with nothing inside of it. That will create a dictionary.
We'll learn about dictionaries in our next video, but just realize that it's a different type of variable.
So if you want to create an empty set, then just do this. Do set, and then don't put anything in it.
So use the command to turn something into a set, but with nothing there, and you will have created an empty set.
We said that sets
cannot have duplicate members. So if up here, I tried to add another member that was already present, and we run it,
you'll notice that it still just has the same three as before. It essentially ignored that other member because it was a duplicate entry, okay?
Because they cannot have duplicate entries, that makes them really useful for doing things like removing duplicates from a list.
So let's create a list over here. So we'll say that y is equal to, now we're going to use lists,
we're going to use square brackets, and we could do numbers, or words, or whatever. Let's do numbers though. We'll do these following numbers.
So there are a couple duplicates in this list. You can see them. We have multiples twos, multiple threes, right?
So to get rid of them, there's two ways we can do it. We could do something called list
comprehension, but we haven't learned how to do that yet, and it's actually a little bit more complicated.
We'll get to it soon.
But we could also just convert this list into a set, and since sets have the property of not having any
duplicates, it will get rid of them. So let's go ahead and do that. So down here, we're going to say that
y is now equal to, we're going to do set of y. So what the list was before, we're going to convert it to a set.
So let's run this, and we see that y has now been converted to a set, and it only has one, two, three, four, five, six.
Those are all unique elements inside of there, okay?
So it's a really great way of getting rid of duplicates, which is going to be important for a lot of data processing,
removing duplicates, right?
So remember that you can only add immutable things like a string or a tuple. If you try and add a list, you will get
an error, okay?
You can remove duplicates. That is possible. These are mutable, like we can change
things in our set. For example, up here, this x has the elements cat, dog, and fish to it.
We could add or remove. So let's add something. Let's do x.add. That's how you add something to it, and we'll say bird, right?
We can add a bird to it, no problem, and sure enough, bird will show up on here, okay?
And it just alphabetizes them, okay, in this list. Notice how it didn't drop at the end, it just put it at the front.
Let's try doing a zebra, see where it puts it. It puts zebra at the end, okay?
You can also remove things, though, right? We could go x.remove, and we could remove the zebra.
However, be careful when you use the remove command, because if you try and remove something that's not there, like let's try and remove
horse, right? Horse wasn't in our list, and so if we try and run it, you're going to get an error.
It will throw an error. There's another thing that you could do, though. If you come down here,
there is a backup. You can do discard, and if you try and discard something that's not there,
you won't get an error, right? So we will try and discard horse from our list, and
no problem. We still have our set just like it ought to be. Okay, what else can we say about these?
It is possible to make something called a frozen set, which is a set that cannot change, right?
So there's some instances where we'll need to use those. The syntax is pretty straightforward. We take our list, right?
So let's down here, let's take a new list right there.
We're going to call it z, and we're going to create a frozen set out of this.
So frozen set out of z. When we run this, we now have a
immutable one. All right, what else can we say?
We've got lots of useful methods that can be applied to set. We've already talked about add, remove, discard.
There's update, and this will add multiple items to a set at the same time. Now, there's some really important
operators that have to do with figuring out what the overlap or lack of overlap is between sets. The different methods that we can use are
union, intersection,
difference, and symmetric difference. And here's what they mean.
Union takes all of the elements from set A and combines it to the elements from set B, so it grabs all of them.
Whereas intersection only takes the things that these two have in common. So if you've seen a Venn diagram, right?
This is the overlap in a Venn diagram. That would be the intersection method.
You can take the symmetric difference, which would only take the things that they don't have in common, right?
And then you can take the difference, right? Or sometimes we call this the relative complement of one to the other.
So for set A, if we apply the method to set A using set B,
it will take away from set A everything that's in set B, right? So these are really useful ways of
basically doing data wrangling and managing big amounts of data in computer programming.
These are really, really useful because they save you a ton of time of trying to match things together
because it does it for you using these sets, right?
There's some other useful methods. For example, there's isSubset, and that will tell you whether all of the members B are part of A.
So again, if this was a Venn diagram and we were to draw these different things, if let's say that this is the set A,
and then set B was completely inside of A, then we would say that B is a subset of A.
And so this will tell you whether that's a true or false statement, this method.
And then similarly, there's this one that says isDisjoint, and that will tell you if two sets have no common elements.
So if you say isDisjoint, you know, set A, isDisjoint set B, if it answers true, that means that they have nothing in common.
So let's do some examples of these because I think it's pretty useful.
So we're going to come over here, and we're going to take this.
What I've done is I've gone to the Internet Movie Database,
and I have grabbed all of the keywords from a couple different movies.
I grabbed it from Lord of the Rings, I think Fellowship of the Ring.
I grabbed it from Star Wars, the very first one that was made, and from the very first Harry Potter.
So I grabbed all those keywords, and what you're seeing in this code is some stuff that we haven't learned about yet.
For example, I imported pandas.
That's a library that helps us work with Excel spreadsheets.
I'm bringing that comma-separated-value spreadsheet in, right?
I'm reading it into a data frame, a pandas data frame.
And again, we're going to have a whole future video on pandas data frames, so don't worry about it for now.
And then I'm bringing in those keywords, and I'm storing them as lists.
I'm just calling one list L-O-T-R for Lord of the Rings.
I'm grabbing all those, and I'm dropping N-A.
That means I'm dropping any empty cells.
I'm doing the same thing for Star Wars, the same thing for Harry Potter.
So let's just work our way through this.
I'm going to F9 my way through this, so running it line by line.
And when we get to here, we now have the following, right?
I have series, so lists.
Essentially, I have series inside of these.
And here's all the different keywords for the Harry Potter, right?
So these are there.
We can go ahead and convert these to sets, right?
So if we run this, we can now convert all of those over to sets.
So you see that this is now a set, okay?
So now that they're sets, we can see what these three movies have in common, right?
We can use our commands that we did previously.
For example, we'll say L-O-T-R and Harry Potter common, right?
So the things that they have in common, what do we need to do?
Well, that's going to be a, which one of our commands?
We come over here.
What do they have in common?
That's going to be an intersection.
So let's do that.
So we're going to take L-O-T-R.
We're going to do the intersection method, intersection.
And we're going to do that with Harry Potter, okay?
When we run that, let's see what we get.
Okay, what Lord of the Rings and Harry Potter have in common.
Let's take a look at this.
Here's the keywords, fantasy world, sorcerer, goblin, good versus evil.
It's a much smaller list, right?
The size of this is only 21, whereas Harry Potter had 319 keywords,
and Lord of the Rings had 106 keywords,
and we now know that there are 21 of those in common.
So we could do the same thing.
Let's grab the same line of code,
and let's see what Star Wars and Lord of the Rings have in common.
So we're going to do the same thing.
We're going to take the Lord of the Rings intersection.
This time, instead of Harry Potter, we're going to take Star Wars.
And when we run that, we find what they have in common is going to be also 21,
but different ones.
It has some slightly different ones.
It's got ambush.
It's got a few different things.
Foot chase.
It's got some different things in it, right?
And then we can actually do this method.
You can do it to more than one.
You can do more than compare two sets.
You can compare three or more sets.
So let's do that between all of them.
So we'll call this all three in common, right?
So this is going to be we're going to start with Lord of the Rings,
but we could start with any of them.
We're going to do the intersection of both Harry Potter and Star Wars.
And because these are all sets, it can do that command.
So when we run it, here's what we find all three in common.
There's nine things that they all have in common.
Monsters, cult films, self-sacrifice, mission, no opening credits, thanks, IMDb,
epic hero rescue, good versus evil, right?
So that's an example of using the intersection command.
We could do differences though, right?
What is the set of differences between set A and set B?
Let's try that one.
So let's do down here.
We're going to do, we'll call this diffs.
This is going to be what are the differences between,
let's do the differences between Lord of the Rings and Harry Potter, right?
So this is going to be, what's our syntax?
It's going to be difference.
Okay, so let's run that.
The diffs we see are all of these things.
These are differences.
Or in other words, these are things that are in A that are not in B.
So none of these better be in Harry Potter.
Yeah, I don't see anything of these that are in Harry Potter, okay?
And then we could do symmetric difference,
which is just going to remove the things they have in common.
Union would join them together like we could do.
So let's make a new variable, union of fantasy, right?
And this is just going to bring all of them together.
We're going to start with one of our sets.
We're going to do union of Harry Potter and Star Wars.
Somewhere out there, someone's brain is exploding,
so we brought all those things together,
but now they have all 776 of these things all present in them, okay?
So that is working with sets.
It makes it really interesting to figure out where your overlap is
and compare different lists of things, okay?
Now, we are also going to talk about tuples in this video.
Tuples are really interesting.
Remember, we define those as a collection,
which is ordered but unchangeable.
So it's ordered.
We have indexes with them, but you cannot change this.
So it's kind of like a list that can't change, right?
And it does allow duplicate members, just like a list, right?
We write tuples with round brackets,
or you can just use comma-separated values.
So let's come over here and do an example of that.
So for a tuple, we could say that we'll call this new tuple
is equal to, we're going to use round brackets,
and we could just do it like that, okay?
So this will have created a tuple.
So let's run that.
I'm going to get rid of variables just to clean this up a little bit.
Let's run it again.
Okay, here's our new tuple.
See, it's called a tuple, and it's 1, 2, 3, 4, 5, right?
Now, what do we know about this?
It says that these are unchangeable.
So let's try and remove that, right?
Well, first off, let's ask for one of them at a given position.
We'll do new tuple, and then we want to know one at the fourth index 4, right?
So when we run that, it tells us, well, that's 5, of course.
So now let's try and pop that one.
Let's do new tuple.
And normally, you could just do .pop,
and then pop the one at that index 4 position,
and it would get rid of it.
But you cannot do that with a tuple.
Tuples are unchangeable.
You can't get rid of the things in them, right?
They're permanent, basically, okay?
So that's why it threw the error here and didn't allow it, okay?
What else can we write about them?
It is possible to create nested tuples, right?
A tuple with tuples inside of it is totally allowed to do.
So what if you really need to change something?
If you need to change a tuple, you could convert it to a list,
change it, and then convert that list back to a tuple if you need to.
So why bother with this?
What's the use of this?
Well, tuples can do some cool things.
For example, when you make the assignments for a tuple,
they do it simultaneously rather than sequentially.
That may not seem like a big deal,
but that allows us to do some cool things like swapping values.
Let's do an example of that.
We're going to come down here, and let's make some variables.
Let's say that the variable A is equal to 5
and that the variable B is equal to 6, right?
Now, normally in your code, if you wanted to swap the values of those,
think what's going to happen.
If I say, like, B is equal to A, right?
Then let's go ahead and just run this step-by-step and see what happens.
So F9, F9, now we have A and B, 5 is equal to 6.
Now right here, if I set B equal to A, B is now 5,
but if I try and do A is equal to B, look what happens.
Like, they're still both equal to 5
because we deleted that value of 6, right?
Because that's a problem.
So the way that you normally get around this with coding
is you do something like this.
You say, like, okay, A, you say temp,
like some temporary variable is equal to A, right?
And then you say A is equal to B,
and then you can say B is equal to temp.
So let's try that one, okay?
So let's go step-by-step through this.
We've got A and B defined as 5 and 6.
Now we're going to create a new variable temporary, like 5.
Then A is equal to B, and B is equal to temp.
So we switch those.
Instead of 5, 6, it's now 6, 5.
It's kind of a pain.
Tuples let us get around this in a really clever way, right?
We can do these instead.
So here's how it works.
You can assign them simultaneously.
So let's use tuples for this.
Let's say that A and B are equal to 1 and 2, right?
So let's try that.
So now we've assigned them A and B,
the values of 1 and 2, by putting them in a tuple, right?
By using this tuple, rather.
And now you can do something called unpacking.
So unpacking is something that tuples can do,
and it's pretty slick.
Here's how it works.
When you unpack something,
like let's say we had our tuple from before,
we had our new tuple,
and it had these 5 values to it.
If you assign something with 5 positions to it,
it will grab those 5 values.
Let's do an example of this, right?
Let's say we have our fellowship, right?
Who's in our fellowship?
Well, we're going to make this a tuple.
That means we're going to use round brackets, right?
And then let's put a list in in our tuple, right?
So the first list, we're going to put our hobbits.
So we're going to put Pippin.
We're going to put Mary.
We're going to put Frodo.
We're going to put Samwise, okay?
Now, in addition, we have not just the hobbits,
but we have our humans.
So let's make a list of our humans.
We've got Aragorn.
We've got Gandalf.
By the way, if you're going over,
you should just hit enter, right?
It's not good practice to have it go, like,
way beyond outside of the screen,
even though it'll still run.
And we've got Boromir, okay?
So now we've got our humans.
We're almost there, so let's go ahead and add our...
There's only one dwarf, so Gimli.
And then we've got one elf, Legolas, okay?
So if we go ahead and run this,
we now have a tuple representing our fellowship.
And in this tuple, take a look here.
It has a size four because in one of our positions,
we have four characters.
In another one of our positions, we have three characters.
See how this is?
So it's sort of nested.
It's lists nested inside of our tuple, okay?
Now let's try and unpack this.
So over here, we're going to assign four categories.
We're going to assign the hobbits, right?
The humans, the dwarves, and the elves.
And we're going to assign that to our tuple.
And it's going to unpack it.
It's going to say, all right, in my tuple,
if four things are getting assigned to it,
it's going to assign these to the first one,
these to the second, third, and fourth, respectively.
So let's take a look at this.
So let's run our whole code, top to bottom,
and we're going to see a bunch of new variables.
In our dwarves category, it put Gimli.
In our elves category, it put Legolas.
In the humans category, it put our three humans.
In the hobbits category, it put the four hobbits, right?
So this is pretty cool.
That's a really useful thing that tuples can do, right?
So how does that help us do this whole switching of variables thing?
It's really easy, right?
Here we can assign the variables,
and then if we want to switch them,
because the assignment happens simultaneously,
we don't have to worry about finding a temporary variable.
We're just going to do BA, right?
So the variables A and B are going to be equal to the variables B and A.
We don't have to ever assign them to a temporary one.
So again, right now, the variables A is 1, B is 2.
This should reverse it, so let's try that.
Let's run it.
Sure enough, A is now 2, and B is 1.
So for swapping variables,
this is a really helpful thing that tuples can help us do, okay?
So that is sets and tuples.
Next up, we'll talk about dictionaries,
and then we're ready to move on to different operators
and apply operations to all these things.