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
We’ve just learned how to turn simple loops into a 1-line list comprehension. But what about loops that have conditional if/else blocks? List comprehension can handle that too!
The Syntax
# Condition affects the list
[expression for temp_var in iterable if condition]
# Condition affects the expression
[expression if condition else 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
We've just learned how to turn simple
loops into one-line list comprehensions.
0:00
But what about loops that have
conditional if else blocks?
0:06
List comprehensions can handle that too.
0:10
There are two types of conditional list
comprehension syntaxes, and it depends on
0:13
whether the conditional effects the
iterable, or if it affects the expression.
0:18
When a conditional affects the iterable,
0:23
the number of elements of the iterable
might change based on the condition.
0:26
Taking our welcome banner, for example,
0:31
we might want to filter out any letter
that isn't a vowel from the final string.
0:34
Or with our name tags, we might want to
filter out any name that has less than
0:40
six letters from our final list.
0:45
To include this kind of conditional,
our syntax only changes a little bit.
0:49
It looks just like our basic
list comprehension syntax, but
0:56
with an additional if condition section
before the closing square bracket.
0:59
Let's give it a go together.
1:06
Open up conditional list.pi.
1:08
You'll see two for loops, and the
conditional list syntax we just learned,
1:11
as well as some print
statements at the bottom.
1:16
The first for loop will iterate
over old_nametags, and
1:19
only append names with six
plus letters to new_nametags.
1:23
The second for loop will
iterate over old_banner, and
1:29
only append vowels to new_banner.
1:32
Let's first run this file to see
what output we're aiming for.
1:36
With Python3, 3 tab.
1:40
We can see that Laura's name
has been filtered out, and so
1:45
has all the consonants from our banner.
1:49
Let's refactor these
using list comprehension.
1:52
First, let's uncomment this template,
and we'll start replacing parts.
1:57
Original iterable, as before, is our list.
2:02
Temporary variable is our nametag,
2:08
and expression is simply this nametag.
2:12
And finally our condition is if the length
of the name tag is greater or equal to 6.
2:18
So it's simply this line
of code right here.
2:24
Great, you've just written your first
conditional list comprehension.
2:29
Let's comment out the loop.
2:34
Save the file, and run conditional_list.py
by pressing up and enter.
2:38
And we can see that the output
is exactly the same.
2:46
Moving down to the banner, again,
2:51
let's first comment the template,
and then start replacing.
2:55
Original_iterable is our welcome
banner in all lowercase.
3:01
Temporary_variable is our
letter from the for loop.
3:06
An expression is just the letter.
3:11
Finally, our condition is if
the letter is a vowel, so
3:16
we can simply take this conditional
statement and pop that in.
3:21
And there you go, your second conditional
list comprehension is written,
3:26
just like that.
3:30
Since this is a string,
let's remember to use the join method.
3:33
We'll supply the empty string glue,
joins syntax, and the wrapping brackets.
3:38
Let's comment out the loop,
save the file and run it.
3:45
Nice work.
3:54
When a conditional affects the expression,
3:56
we'll only perform the action
on certain elements.
3:59
In our welcome banner example,
we might only make the vowels uppercase,
4:03
but keep all other letters lowercase.
4:08
With our name tags example, we might
only capitalize the names with six
4:12
plus letters, and
keep the rest all lowercase.
4:16
This kind of conditional has
a slightly different syntax.
4:21
Again, we start with the basic
list comprehension syntax,
4:26
but now we've added a conditional
statement after the expression.
4:29
We also have an else keyword as well now.
4:35
Let's see it in action.
4:38
Open up conditional_expression.py.
4:39
As always, we have two loops,
the syntax templates,
4:44
as well as the print statements.
4:48
This time the conditional is changing
the value in some way before
4:51
appending it to a new list.
4:56
For our name tags, we're capitalizing
the name if it has six plus letters,
4:59
and leaving it as is if otherwise.
5:04
For our banner, we're making all vowels
uppercase, but leaving the rest as is.
5:08
No changes are being made
to the number of values.
5:14
Let's run conditional_expression.py
to see what output we're aiming for.
5:18
With Python3 4 tab, enter.
5:24
We can see that only Dustin and
Rachel are capitalized, and
5:29
welcome now features uppercase vowels.
5:33
Let's get right to work refactoring
these loops into comprehensions.
5:37
First, let's uncomment the template for
our name tags, and
5:43
start slotting our values in.
5:48
Original iterable is our list,
Temporary variable is our nametag.
5:50
And our condition is if the length of
the name tag is greater or equal to 6.
6:00
Now this first expression is what we want
the value to be if the condition is true.
6:08
So, we'll use nametag.capitalize
if the name has 6 or more letters.
6:13
And the second expression is if
the conditional evaluates to false.
6:22
So it's simply nametag
without getting capitalized.
6:27
Let's comment out the loop and
run the file to check it out.
6:33
Comment this out, remember to save,
and we'll press up and enter.
6:37
Nice work, we now have a new list
that contains all the values
6:43
from the original list, but
some values have changed.
6:47
Next is the welcome banner.
6:53
As always, let's uncomment
the template and get replacing.
6:56
Original iterable is our welcome string,
7:02
temporary variable is our letter,
7:07
condition is the if letter is in a,
e, i, o, u.
7:11
Now the first expression
is if it evaluates to true.
7:16
So we'll grab this letter.upper because
we want our vowels to be uppercase.
7:20
And the second expression is
if it evaluates to false,
7:26
so simply this letter for any consonants.
7:30
As always, we'll comment out the loop,
save the file and run the file.
7:33
Whoops, I forgot my join syntax,
let's add that in with our empty glue,
7:42
join method and wrapping brackets.
7:47
We'll save the file again, run the file.
7:52
And our welcome banner looks as it should,
and it's looking very fun.
7:57
Great work, you've just learned how to add
conditionals to your list comprehensions.
8:01
You now have more control over
the values that go into new lists,
8:07
and which values get actioned upon.
8:11
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