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
Lists are not the only iterable in Python, so letβs see how we can use comprehension to create dictionaries
Resources
The Syntax
# basic
{key:value for temp_variable(s) in iterable}
# with a conditional
{key:value for temp_variable(s) 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
So far, we've looked at loops that
take an original list or string, and
0:00
outputs a new list or string.
0:05
We've been using list comprehensions
with the square brackets,
0:07
because we want our output to be a list or
list to revert it back to a string.
0:11
But, what if we want our return
value to be a dictionary?
0:16
Luckily, dictionary comprehensions
are also a feature of Python.
0:20
Let's say, we want to create a dictionary
that stores integers 1 to 10 and
0:25
their squared values.
0:30
Using a loop, we can iterate through each
number produced by the range function, and
0:32
then add each number and its squared value
as the key and value of a new dictionary.
0:38
Here, we're using a list and
its values to create a dictionary.
0:44
We can also create a new dictionary
from an existing dictionary.
0:50
Let's say, we have a dictionary containing
products and their prices in USD.
0:54
We may wanna produce another
dictionary with the exact same products,
1:01
but with the prices in AUD.
1:05
This can be done with a loop by
stepping through each key value pair
1:08
of the dictionary,
applying the exchange rate, and
1:12
then adding it as another key
value pair in the new dictionary.
1:15
Since both of these can
be done with loops,
1:21
then they can probably be
done using comprehensions.
1:23
This is the dictionary
comprehension syntax.
1:27
Let's break it down.
1:31
The curly brackets are used to hold
the values generated by the dictionary
1:33
comprehension.
1:38
Python dictionaries
are wrapped in curly brackets.
1:40
So, dictionary comprehensions are the same.
1:43
Iterable is the same as before.
1:47
This is the original list or
dictionary that we're pulling data from.
1:49
Temp_ vars is similar to the list
comprehension temporary variable.
1:54
It's what we've looped to
in the original iterable.
1:59
We'll explain why there
might be multiple soon.
2:04
Key, is an expression that returns
what we like to use as the key for
2:07
each key value pair.
2:12
Similarly, value is an expression
that returns what we'd like to use as
2:14
the value for each added key value pair.
2:19
Both of these will make much more
sense once we start working with it.
2:23
So, let's dive right in.
2:27
Open up dictionary comprehension.py.
2:30
Just like before, we have two loops, two
syntax templates and two print statements.
2:34
This first loop, creates a dictionary
that stores numbers 1 to 10,
2:40
produced by this range method and
their squared values.
2:44
It loops through each number, assigns
the temporary variable num as the key and
2:49
then its squared value num to
the power of 2 as the value.
2:54
This second loop, takes a dictionary
of products with prices in USD
3:00
nd creates a new dictionary of
the same products with prices in AUD.
3:05
It loops through each item in
the dictionary, unpacks the product and
3:11
price using items, and
3:15
then uses those values to assign new
key value pairs in the new dictionary.
3:17
If this dictionary looping syntax
looks new, be sure to check
3:21
out the teacher's notes for the course
where this was first introduced.
3:25
Let's run this file to check out
the expected output from both loops.
3:31
With Python3, 5 tab.
3:37
And there we go.
3:41
Now, let's refactor these to
dictionary comprehensions.
3:43
First, let's tackle the dictionary
containing squared values.
3:49
Let's uncomment this template and get
replacing. Original iterable is this range,
3:53
temporary variable,
is this num from our loop.
4:03
Key expression,
is this key right here, num.
4:10
And value expression is the squared value,
so num to the power of 2.
4:15
Let's test this out.
4:22
We'll comment out the original loop,
save the file, and then press up, Enter.
4:24
Great, everything looks the same.
4:32
Now let's refactor the dictionary
to dictionary loop.
4:35
We'll come down here, uncomment
the syntax template and get replacing.
4:40
Original iterable is this
dictionary right here,
4:46
plus the items method to make sure
we can unpack the product and price.
4:50
The key and value is what we've got here,
product and price.
4:57
Key expression is simply
this product right here.
5:04
And value expression is price
times the conversion rate.
5:08
Now for this,
we will use the raw value of 1.49 so
5:12
we can keep everything in one line.
5:17
Let's comment this out,
save the file and try running it.
5:20
It's looking good.
5:27
One more thing we can do here,
5:30
is to apply our conditional
knowledge from the last video.
5:32
Take some time now to try and
5:37
add only the products that are under
5 USD to the new dictionary.
5:39
Pause this video and try to work it out.
5:44
I'll wait.
5:47
Here's what I did, I just added,
5:50
if price is less than 5 to
the end of the comprehension.
5:53
Now, it will loop through the products,
filter out the products where the price is
5:59
greater than 5, which is our butter and
convert the rest.
6:03
Let's save it and see it in action,
and there goes the butter.
6:08
Let's try another challenge.
6:14
Pause the video again and
6:16
try to add only products that
are under 5 AUD to the new dictionary.
6:17
This is the price after we've applied
the exchange rate, I'll wait.
6:23
Here's how I went about it.
6:28
I simply added the exchange
rate to the conditional,
6:31
price times 1.49, super easy, right?
6:36
Let's check it out.
6:40
And now we only have our parsley.
6:43
Great work.
6:45
You've learned about the dictionary
comprehension syntax.
6:46
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