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 Comprehensions!
You have completed Python Comprehensions!
Preview
A set is another iterable that can be created using comprehension. Letβs dive into its syntax!
The Syntax
# basic
{expression for temp_var in iterable}
# with a conditional
{expression for temp_var in iterable if condition}
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
Another Python datatype
that can be created using
0:00
comprehension syntax is a set.
0:04
Since a set is so similar to a list,
0:07
the comprehension syntax
is almost identical.
0:10
Here's the syntax for
a list comprehension.
0:15
And here's the syntax for
a set comprehension.
0:19
It's easy to miss, but our square
brackets are now curly brackets.
0:23
Python set syntax uses curly brackets.
0:28
So naturally set comprehension
syntax will also use curly brackets.
0:32
Let's explore set comprehension.
0:38
Open up set comprehension.pi.
0:42
As always, you'll see two loops,
two syntax templates, and
0:45
two print statements.
0:49
We have a pretty common use of
sets here in the first loop,
0:51
removing duplicate values.
0:55
We have a list of test scores, and
0:57
we're looping through each one
to add to the unique scores set.
1:00
Any duplicates will be removed
due to the nature of sets.
1:05
The second loop adds a condition and
1:10
will only add scores that
are greater than 70 to the new set.
1:12
Let's see them both in
action by running the file.
1:17
Python3 6, tab, enter.
1:21
Our first set has removed
the duplicate 74, and
1:26
our second set only contains
scores higher than 70.
1:31
Let's jump into refactoring.
1:36
We'll first uncomment the template syntax,
and off we go.
1:39
Original iterable is our
list of test scores.
1:44
Temporary variable is our score.
1:48
And our expression is simply score yet
again.
1:52
Super easy,
just like our first list comprehension.
1:57
Let's refactor the other
one before we run the file.
2:01
Now, instead of plugging
in variables again,
2:05
we can just grab the set comprehension
from the first example and add to it.
2:08
So let's do that.
2:13
Grab the set, uncomment this,
and just replace it.
2:14
Now we know there's a conditional here,
but which type is it?
2:22
And where would it go
in the comprehension?
2:26
Pause the video and
take some time to give this a go yourself.
2:29
I'll be here.
2:32
How did it go?
2:36
Here's what I did.
2:38
We want to filter out
everything less than 70.
2:39
So the conditional affects the list.
2:43
The condition will therefore be
at the end of the comprehension.
2:45
Let's add it, if score is over 70.
2:49
Now, let's save the file and run it.
2:55
And we've got an identical output.
3:00
Good refactoring.
3:02
You can now add set
comprehensions to your tool belt.
3:04
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