Understanding Comprehensions
00:00
In the previous lesson, I reviewed Python’s dict
class. In this lesson, I’ll introduce dictionary comprehensions, a single line way of constructing a dict
.
00:10
A Python comprehension is a bit of shortcuts and syntactical sugar that allows you to construct a data structure. Both the list
and dict
types have a comprehension form.
00:20
Both forms use syntax similar to a for
loop, but nested inside the syntax brackets for the respective data structures. So for lists, that’s a square bracket, and as you saw in the previous lesson, it’s braces for dicts
.
00:35
This is the structure of a dictionary comprehension. I find it easier to read a comprehension from the inside out. The underlined chunk here starts in the middle, is the same as the declarative part of a for
loop: for thing in container
where thing
is the variable getting stuffed with the contents from the container.
00:56
In Python, this process is called iterating, and an iterable is anything that can be iterated over. member
in this case is the target of the iteration.
01:06
The portion here to the left of the loop declares the key-value pair that is to be inserted into the dictionary. This is almost always something based on that iterator target in the for
.
01:18
So key
and value
would be constructed based on member
in this particular line.
01:23
There’s another more complex form of dictionary comprehension as well that allows a conditional. The if
here is like a Python if
statement, and if the condition evaluates to true, then the key-value pair gets included.
01:38 Otherwise, you proceed to the next step in the iteration. Let’s head into the REPL and try this out.
01:46
On the screen is the powers_
of_two
example from the previous lesson, where I used a for
loop to create a dictionary containing the first nine powers of two.
01:55 Let’s try this as a comprehension instead.
02:05
Remember how to read this. Start in the middle at the for
. for num in range()
means to iterate over the return result of the range()
function, which is the numbers one through nine.
02:16
Each value from range()
is put into num
, and then on the left side of the comprehension you have a key-value pair. The key is the value num
, while the corresponding value being stored is 2
to the power of num
.
02:32
As you can see, this has the same result as the for
loop above. This code is more compact than a for
loop. It’s a tiny bit harder to read if you’re not used to it, but it also tends to be faster.
02:44
Since comprehensions are more restrictive than a general for
loop, Python can do things to optimize them. Most of the time, a comprehension will be faster than its loop-based equivalent, but not always.
02:55 You should always validate. Let’s do this again, this time with a condition.
03:09
Once more, when reading it, start in the middle at the for num
part. I’m still iterating over the result of range()
, which is the numbers one to nine.
03:19
To the right of that is my if
condition. If the number’s even, that’s mod 2 == 0
, then the key-value pair gets included. If it’s not, then it doesn’t.
03:30
On the left-hand side is the key-value pair. This is the same as it was before mapping num
to 2
to the power of num
, and there’s the results.
03:42 You can also nest the iteration portion within another iteration portion.
03:47 Consider a matrix defined as a list of lists.
03:59
This is my matrix with three rows and three columns containing the numbers from one through nine. If I want to create a dictionary containing the items in the matrix as keys and their squares as the values, I can use a nested for
mechanism inside of for
mechanism in the comprehension.
04:24
When reading this, once more start in the middle, or in this case a bit left of center at the first for
statement. This time, the for
is iterating over each row in the matrix list of lists.
04:36
To the right of that is a nested for
statement, which iterates over each item in the row and puts the result in num
. On the left side, you get a key-value pair. The key is num
, the item from the matrix, while its corresponding value is its square.
04:59 Next up, I’ll show you some comprehensions you might use to solve specific problems.
Become a Member to join the conversation.