Exploring Comprehensions From Iterables
00:00 In the previous lesson, I introduced you to dictionary comprehensions. This is the first in two lessons that show examples of how you can use them. Dictionary comprehensions often get used as calculate and store mechanisms.
00:13 You might start with data in one format, like a list of items, and then use a comprehension to create and store related information. The first example I’ll show you starts with a list of strings and creates a dictionary that pairs those strings with their lengths.
00:29 Let’s head into the REPL and look at that now. I’ll start out with a list of names. When
00:39 creating a comprehension, both the key and value portions of the expression can include function calls.
00:51
This comprehension iterates over the names, but what’s new is that both the key and value are calculations. The key is the result of calling .upper()
on name
while the value is the length of the name.
01:03 This pattern is quite common. String lengths are quick things, so this example’s a little contrived, but consider anything where you want to do a calculation that takes a little longer.
01:13 You can call a function to return a resulting value, which you store in the dictionary, and then all future use of that value is simple and quick dictionary lookup instead of the more expensive calculation.
01:25 For another example of this, let’s generate an ASCII table, a dictionary that maps the lowercase letters to their equivalent ASCII codes. First off, I need the letters for the table.
01:40 I could have just typed them in, but why do that when Python provides a string containing those values already? And there they are, all the lowercase ASCII values.
01:51 Remember, when you iterate over a string, you get each character inside of it one at a time.
02:04
Here, I’ve iterated over the lowercase letters and called the built-in ord()
function, which returns the ASCII value. Technically speaking, it returns the Unicode code point, but for the lowercase ASCII characters, that’s the same thing.
02:17 Now I’ve got a dictionary with all those values. So far, the iterables you’ve seen being processed have all been simple, but the iterable portion of the comprehension can also be a function call.
02:32
Consider this list of parts and a corresponding list of the number of those parts in your inventory. The built-in zip()
function can take these two lists and pair their corresponding items together.
02:46
Hmm, that wasn’t so helpful. zip()
returns a zip object, which is iterable, but that isn’t clear on the REPL. Converting the result to a list iterates over it to stick its values in that list. And this is what I was talking about, a set of tuples where each tuple pairs the part name with the amount of stock of that part.
03:09 I did this to show you the concept. Now let me put this together in a comprehension.
03:20
When reading this comprehension, let’s start on the right-hand side with the iterable, which is the zip object returned by the call to zip()
as each item in the iterable is a tuple, you can dynamically dereference the parts of the tuple into values, which I’m doing by saying for part, num
.
03:38
Now I’ve got a part
and num
variable that I can use inside the key-value portion, which in this case, I’m just straight out storing as a key and value.
03:47 I did all this to demonstrate the fact that you can put a function call in the iterable portion of the comprehension. That being said, there is a better way than a comprehension to accomplish this particular piece of code.
03:58
The dict()
constructor also accepts paired tuples and will turn each tuple into a key-value pair inside the dict()
. So instead of a comprehension, I could have just done this.
04:12 Of course, this only works if you have the tuples directly. If you want to do some sort of calculation, then you’re back to comprehensions. Consider a third list containing the price of each item.
04:25 Now, if I want to find the total value of each part in inventory,
04:39
I have a longer zip()
on the right-hand side, and the same unpacking of the zip tuple in the middle, this time into three values. The key-value portion now contains a calculation mapping the part to the total value of the parts in inventory by multiplying the price of the part by the number of the parts there.
04:59 Sometimes you have a dictionary containing data you need in a different format for better access. In the next lesson, I’ll show you some comprehensions that do dictionary transformation.
Become a Member to join the conversation.