This course will be retired on January 6, 2020.
Bummer! This is just a preview. You need to be signed in with a Basic account to view the entire video.
Start a free Basic trial
to watch this video
Sets are a great Python collection type that a lot of people don't know about.
One of the most common ways you'll see sets being used is to make some other iterable unique. For example, say you have a list of page numbers where terms appear in a book. Since some pages could contain multiple terms, you're likely to get repeats. In that case, you'll see people doing code like this:
pages = list(set(pages))

0:04
There's a collection data type in Python that a lot of people don't know about or

0:08
don't know very much about, this is the set type and

0:10
if you're familiar with set theory in math.

0:12
You probably have a few ideas about how this data type can be used

0:15
if you're not and trust me I am no expert in this at all.

0:18
Let me try to explain a few basics.

0:20
A set is a collection of unique items that belong together for some reason.

0:24
And really this reason can be anything.

0:26
Maybe it's a set of prime numbers up to a hundred or maybe it's a set of

0:29
default type faces on Windows 95 or just the set of your favorite foods.

0:33
Each thing can only be in a given set once because well you can't have

0:36
two installed aerials or two twenty three, sets can also be compared.

0:40
So for example comparing a set of my favorite foods and

0:42
your favorite foods would make a new set of the foods that we both love.

0:46
Python sets are iterable collections like list and tuples, but

0:49
each item is unique and the set doesn't have any indexes.

0:52
Let's go play with them some in workspaces.

0:55
Now it used to be the case that you could only create sets by using

0:58
the set function.

0:59
You have to do like set one three five.

1:04
And I give you a set, but in modern Python versions.

1:06
You can use the curly braces to directly create a set so

1:09
long as your set has something in it.

1:12
Like one three five, gives you a set of one three five.

1:16
If you try to create a blank set though, you'll get a dictionary.

1:20
So if we did like type, on an empty set, then we actually get a dictionary.

1:25
So in that case you have to use, the set function.

1:28
So like.

1:30
Typeset gives you a set.

1:32
Now the order that you put things into sets doesn't matter, either.

1:35
Like I said before sets don't have a defined order or any indexes.

1:39
So you can do like one, 11, 13,

1:43
seven, five, three and we get them back in a different order.

1:48
Now, Python sorts the sets in a way that makes sense to Python.

1:51
Now here it looks like it's doing them a natural order but you will see times where

1:55
it doesn't look like a natural order for how your sets are sorted.

1:59
Okay, so we can make a set.

2:00
And now how do we add or remove things from a set.

2:03
Let's do like low primes.

2:04
There's gonna be a set of one three five 7,11 and 13.

2:13
And if I wanna add the next prime number in there,

2:17
I will do low primes dot add 17.

2:21
And now if I look at low primes then there's the numbers.

2:28
We add single things to a set, with the add method.

2:30
Sets our mutable like lists.

2:32
So the set is just updated in place.

2:35
And again like dictionaries you can use update with another set

2:38
to sort of combine them, one cool thing about sets though is that you can actually

2:42
update with multiple sets.

2:44
So I could do low primes update and and pass in a new set which is 19 and 23.

2:50
And then I realize I've left out the two so I'm gonna add in the 2 and 29.

2:55
And now if I look at low primes.

2:59
I've got all my low primes up to 29.

3:01
So that's pretty handy.

3:03
Now what about removing things from a set though.

3:06
I can't delete things with an index because sets aren't sorted.

3:10
Once again making other data types.

3:12
I have a remove method.

3:14
So I can do low primes dot add 15.

3:17
But you know, 15 isn't prime.

3:21
So let's do low primes dot remove 15.

3:26
And now low primes doesn't have a 15 in it.

3:30
And like with lists,

3:31
if I try to remove an item that doesn't exist, I get a key error.

3:34
So low primes dot remove.

3:36
I'm gonna remove 100, which doesn't exist.

3:39
I get a key error 100, just like I would with a dictionary.

3:42
But unique to sets is a method named Discard, and

3:45
I actually wish more data types had this one.

3:47
If I do low primes dot discard, nothing.

3:51
Discard tries to move the value but if it doesn't exist it's just goes on.

3:55
Seriously, Python developers let's get that added to lists and dictionaries.

3:59
You can also pop items off of a set if you needed too for some reason.

4:03
So for instance while low primes, print low primes stop pop divided by three.

4:13
There's our numbers.

4:15
Honestly I don't know the last time I used pop on a set.

4:17
In fact most of the work I've done with sets has involved an entirely new group of

4:21
methods and operators that tie back into the math basis of sets.

4:25
This is a lot to look at though.

4:26
So get a snack and settle in.
You need to sign up for Treehouse in order to download course files.
Sign up