Check out a free preview of the full Practical Guide to Python course

The "Sets" Lesson is part of the full, Practical Guide to Python course featured in this preview video. Here's what you'd learn in this lesson:

Nina demonstrates that sets are mutable data types that store immutable items in an unsorted way. Items can be added and removed from sets. Unlike a list or tuple, a set can only contain one instance of a unique item.

Preview
Close

Transcript from the "Sets" Lesson

[00:00:00]
>> Next up, we have sets. Sets are also mutable data types, and they allow you to only store immutable items in them in an unsorted way. Sets are mutable, meaning that you can add items to them, you can remove items from them. They can contain other immutable types like integers string tuple because tuple is immutable but not lists, not sets and not dictionaries, which are themselves mutable.

[00:00:30]
Unlike a list or tuple, sets can only contain unique items in them. If you make a set and you try to add items from the set into it instead of like a list where you can have as many copies as you want, the set is just gonna toss that away.

[00:00:46]
And the benefit of this is that sets are fast, they have fast membership testing. We'll talk about this a little bit later. But because the items in them are immutable, you can run a function called a hash function and very quickly figure out. Is the item I'm looking for in the set or not without having to look at every single value in it.

[00:01:08]
And you can also use powerful set operations like union intersection difference. I'm not going to jump into these during the course of this chapter, but there are some exercises around them. Quick set cheat sheet here at the top and let's talk about sets. So, if lists have square brackets, and tuples are parentheses and commas.

[00:01:35]
You might think that sets are curly braces But there's an aha here. If we try to create an empty set with just curly braces. We are instead going to end up with a dictionary because dictionaries are also surrounded by curly braces. So if you need to create an empty set in this case, you will want to use the set constructor.

[00:02:16]
And that will give you the empty set that you are looking for. Sets can have one item in them Or multiples if we have a set where I tried to pass in, three, three and four can anyone on zoom tell me, how many items will be in this set?

[00:02:41]
Two, awesome, because sets can't contain duplicate values. If you have a list of names And you wanted to very quickly duplicate this list, you can actually just pass it into the SEC constructor. No more duplicates so one of the very handy use cases for sets. Sets cannot contain mutable types without having to always look at the cheat sheet.

[00:03:25]
How do we know what types are immutable? What types are not? Shortcut to check for mutability, Is to use the built in hash function. Can I call hash on an integer? Yes, can I call hash on a string? Yes, can I call hash on a list? Nope, it's immutable, it's an unhackable type.

[00:03:56]
What happens if I try to have a set with a list in it? I'm going to get that same error on hashable type list. You're probably going to see this exception quite a bit, so I encourage you to remember it. And when you run into exceptions like this, just read them from the bottom up and think where else have I seen this exception?

[00:04:24]
And that's gonna help you get to the root of the problem. Now, sets, don't have an order. So, if I put a bunch of items into a set. There is no guarantee at all, that they will be the same order that I put them into. Because in sets order does not matter.

[00:04:51]
What is going to happen if I try to get the zeroeth item in the set? Can I do this? Nope, it's going to blow up and say is that object is not scriptable that makes a whole lot of sense right sets don't have an order there is no item at the zero with index.

[00:05:14]
And in the same vein, I cannot say my set zero equals some other item because again, there is no order in sets and sets do not support item assignment. If you do want to add and remove from sets, you can use the add operation. And again, because I'm calling this on my set, and my set is mutable that does in fact change it.

[00:05:45]
I can also call the discard operation. And that goes ahead and removes the item from the set. If I had two sets and I wanted to combine them together, I could use the update method. So I could say that colors are red and orange. Oops, I'm going to name this colors and numbers are a set that is one, two and three.

[00:06:27]
So if I look at my colors, I could call colors update and pass in my numbers. And now I have a new set. Member set is mutable I called update on it and that has in fact changed the data in it. There are set operations like difference intersection.

[00:06:47]
I'm not going to cover them but there is a section on those in the set operations cheat sheet on the course website. So set operations are the most useful and most powerful. Feature of sets, you can quickly figure out what items are the same between two sets, what items are different and do that in a very performant way.

[00:07:14]
Next up, we have dictionaries. Dictionaries are a data type that allow us to store key and value pairs. Dictionaries themselves are mutable, but just like sets, dictionary keys can only be immutable, why? Because dictionary keys need to be hashable. Dictionaries allow fast item lookup and fast membership testing.

[00:07:45]
You can quickly say given a key is this key in my dictionary or not? And that's because the hash of the key is compared with the hash of the keys in the dictionary. You don't have to look through each item in that dictionary and say, are you the item that I'm looking for?

[00:08:04]
So can I have a list as a dictionary in a key I'm sorry, a list as a key in the dictionary by asked that question correctly, I would get a better answer. No, right? So a list is mutable dictionary keys can only be immutable. We cannot have mutable data types as dictionary keys.

[00:08:26]
Can we have a tuple as a dictionary key yes, tuples are immutable so we can use them as a dictionary key. I have a dictionary cheat sheet here for you.

Learn Straight from the Experts Who Shape the Modern Web

  • In-depth Courses
  • Industry Leading Experts
  • Learning Paths
  • Live Interactive Workshops
Get Unlimited Access Now