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
Let’s take the loops from the last video and see how we can simplify them with list comprehension!
Resources
The Syntax
# list comprehension to produce a list
[expression for temp_var in iterable]
# list comprehension with .join() to produce a string
"".join([expression for temp_var in iterable])
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
Now, I'll teach you how to use Python
list comprehension to make your code more
0:00
efficient.
0:01
We'll start with the list
comprehension syntax, as promised,
0:03
it's only one line, let's break it down.
0:07
These square brackets are used
to hold the values generated by
0:10
the list comprehension.
0:14
This should look quite familiar,
because Python lists are also held in
0:17
square brackets, for and in our
keywords that are part of the syntax.
0:22
These should also look pretty familiar
since they're part of the for
0:28
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