Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Well done!
You have completed Python Sets!
You have completed Python Sets!
Preview
The second property of sets is that they do not contain duplicate data. Members are uniquely different from one another.
Second Property of Sets
Sets do not contain duplicate values.
Challenges
What are the results of the following expressions?
print(len({3, 2, 1, 2, 3, 1}))
print(len({'hello', 'hello', 'hello', 'sets!'}))
Related Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign upRelated Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign up
The second property of sets is that
they do not contain duplicate data.
0:00
There's no duplicate values.
0:06
Members are uniquely
different from one another.
0:08
To demonstrate this, I'll create a new
set literal with some curly braces.
0:12
And each member of this set
literal is going to be an integer
0:18
separated by commas.
0:23
And I will repeat some of
these in no particular order.
0:25
And let's get this printed out as well.
0:33
So I've got this giant set.
0:43
I wanna print this out in my script.
0:45
So I'll run my script here.
0:49
And ta-da, this set contains only
the unique values 1, 2, 3, and 4.
0:53
Even though I've repeated all of them,
0:59
I've repeated most of these
numbers inside of the literal.
1:03
When Python evaluated and constructed and
1:10
printed out my actual set,
I only have four members in here.
1:13
And I can double-check that by
printing out the length of this set.
1:18
When I run my script again,
I should see the number four.
1:25
That's right, there's only
four members in this set here.
1:30
So now, what exactly is going on?
1:33
I said that Python evaluated,
1:35
constructed and printed out my set.
1:38
And let's demonstrate
with a smaller example.
1:43
We can explore this concept
of deduplication, right?
1:45
This concept that duplicate data
does not get added into a set.
1:51
So with this example,
I'll have a variable called fours.
1:56
And I'll assign this to
another set literal.
2:02
We're gonna have the number
four as an integer, and
2:05
then the number 4.0 as a float.
2:10
Now when I print out my fours set
right here, now I run my script.
2:14
Scroll down here so I can see all my code,
run my script, that's python intro.py.
2:22
What do you think is going to appear?
2:29
I should see a set,
I know that something goes on with
2:32
deduplication, let's find out.
2:36
So I hit Return and I get one element,
I get the integer number 4.
2:42
So why is that?
2:49
What happened here?
2:51
How come it's the number 4 as an integer
and not number 4.0 as a float?
2:52
Well, in Python,
we can use the double equal sign
2:57
to check if a value is
equal to another value.
3:02
So with numbers,
these are easy to compare, 4 == 4.0.
3:08
And I can print this out,
and I should get true,
3:13
because 4 is totally equal to 4.0, right?
3:18
So a set will evaluate each element that's
being added into the set, just like this.
3:23
And under the hood, if the values
are equal to each other, that is to say,
3:31
if the set already added a 4 and
then it looks at a 4.0, what it's gonna do
3:36
is it's going to compare the 4.0
with everything in this set already.
3:42
Since 4 is already in there, they're equal
to each other, so 4.0 does not go in.
3:47
And this kind of leads us to
answer our other question.
3:53
How come it was the integer 4 and
not the float 4.0?
3:57
Well, Python evaluates our
code from left to right.
4:02
So in our fours set right here,
4:07
we've got the 4.0 on the right side,
let's move that to the left.
4:10
And then we'll put 4 on
the right side here.
4:16
And now when I run my script,
I wanna see the fours set printed out.
4:19
And this time it's the float
instead of the integer.
4:26
That's because we've got this 4.0,
4:31
it's now on the left side, so
it gets added into the set first.
4:34
Then when it's the time that
the integer 4 comes in,
4:40
we have a comparison that's evaluated.
4:46
And since they're equal,
the integer 4 is not added into this set.
4:51
So what remains is
the float 4.0 this time.
4:56
So it's all about the order
in creating a set literal.
5:00
The left one will be added in first.
5:05
I've got one more example for you.
5:09
It's a challenge, actually.
5:13
I've got one challenge for you.
5:14
So here's a set literal,
5:17
We've got 4.0, 4,
5:21
5.0 and then 5.
5:26
What do you think will be printed out to
the terminal when I run this script here?
5:30
When I print out my challenge,
5:38
what do you think will be the contents
of this set that appears?
5:40
Go ahead and pause me, try this out.
5:44
Then come back to me
when you've found out.
5:48
So what do you think will
be in the actual set?
5:53
Let's find out.
5:56
4.0, 5.0, if you said 4.0 and
5.0, great job.
5:59
Remember that in a set,
6:06
Python will keep the leftmost element when
it compares two elements of equal value.
6:07
4.0 is equal to 4, but
since 4.0 is farther to the left,
6:13
it gets added into the set first,
and then 4 is ignored.
6:18
Same with the 5.0,
since 5.0 is more on the left,
6:24
the set will keep the float and
then ignore the integer.
6:29
All right, we'll play more with
this leftmost pattern later as we
6:34
learn more about set operations,
but for now, let's review.
6:39
So sets are unordered collections.
6:45
The order does not matter
like it does in a list.
6:49
When we create a set literal,
6:54
the set will collect only the unique
elements, only the unique values.
6:57
If there is any duplicate
data that have equal values,
7:03
the set will just keep one element and
not the rest.
7:07
And the element that is kept for
a set literal is determined by
7:11
whichever element came first on
the left side, the leftmost value.
7:16
You need to sign up for Treehouse in order to download course files.
Sign upYou need to sign up for Treehouse in order to set up Workspace
Sign up