Unlimited Plugins, WordPress themes, videos & courses! Unlimited asset downloads! From $16.50/m
  • Overview
  • Transcript

2.1 What Are Comprehensions?

As you continue to work with the Python language, you will find that often many roads lead to the same destination. The art is in finding which way works better for you.

Comprehensions are one of those alternate routes. Think of a comprehension as a shorthand way to create collections such as lists, dictionaries, and sets. In this lesson I'll explain how they're good for cutting out a lot of extraneous code. I'll start with some naive code that we can improve upon in later lessons.

2.1 What Are Comprehensions?

So one of the first things that I want to talk to you about, when it comes to handling data within Python, is the concept of having to go through some sort of collection or sequence of data within Python, meaning something like a list or a dictionary or a set. And I want you to do something an operation on all the values or key value pairs within those collections or sequences. So for example, let's say you're given a list or a dictionary. And I want you to go through the list and maybe calculate the squared values of all of the individual numbers within the list, or I want you to do some sort of operation on the dictionary. And the first thing especially if you're coming from other languages that you're probably gonna think about doing is looping through all those values within the list, doing some sort of operation and then saving it off to another list or some other data structure that you can then present to me or to whoever the end user is. Now that's not a problem. That's not a bad thing to do. But we're not going to see that in a lot of circumstances or a lot of situations out in the world. You're gonna see another way to do the operation. Now it's going to get you to the same endpoint. It's just going to take a slightly different direction. And that different direction is called the comprehension. Now a comprehension is really nothing more than a way to create or construct a sequence or collection of data based on another collection of data. So I'm gonna give you a list, and I want you to give me another list back of some operations done and all the values there. So I'm gonna take you through a couple of examples, and then I'm going to show you a simple solution using some sort of loop like a four loop. And then I'm going to show you how to solve that same problem using a comprehension. In the beginning it might seem a little foreign, a little strange. But, a little bit of practice and you'll get used to it very quickly, I promise. So I have a couple of examples here. So let's say the first thing I wanna do is I wanna give you a list of data. So let's say the numbers zero through ten. And I want you to calculate the squared values of all of those numbers within that list. And I want you to just print them out to me, show them to me. So, here's a very quick example of how you might do that if you had not really seen or were not familiar with comprehensions yet. So as you can see here I'm creating a very simple list of that is empty called squared. Then just for brevity sake I'm going to loop through a range of zero to ten. So I'm gonna have the range go up to 11 like this. So let's say 4n, which is gonna represent our number in the range up to 10 or 11 exclusive. And then I want to go ahead and append, onto my squared list. The squared value, which is the double multiplication sign of whatever number that is, right? So, that's pretty simple, pretty straightforward. And then, I want you to print out squared. So, that seems pretty simple. So, let's go ahead and go to our terminal here, and I'm, simply, gonna run Python 3 I want to run lists. So as you can see here it seems to work just fine. We get zero through a hundred which are zero one two three. All we have to ten all of those values squared. Now that absolutely works and that will give you the correct answer. So I'm not saying that's the wrong thing, but I'm gonna show you in the next lesson how to tweak that a little bit and do it a little bit more elegantly. Same thing for the dictionary side of the world. So if you followed some of my previous courses. Then you probably are familiar with this one that I like to do, this problem that I like to do as a sentence analyzer. So the basic concept is you have the end user provide a sentence of letters, numbers, whatever characters and then you analyze it. You go through each individual value within that sentence which happen to be characters and you count them. What is the frequency of the characters in that sentence? And then we save them off into like a dictionary, and then there you go. But the problem with it is depending on how you do it and how I've done it in the past, is you may not take into consideration the fact that you could be dealing with upper and lower case letters like lowercase a and uppercase a. Now while that's okay, you may want to combine those values. Because at the end of the day and a is an a and I don't care if it's upper or lower case it doesn't really matter to me. So, how would you go about taking a dictionary. That is a dictionary of letters and the frequency of those letters and then creating another dictionary from that one that's going to combine all the like letters of upper and lower case. So let's go through a very quick example of how we might do that, and this is a very simple example. So we have a character count dictionary. We have five lowercase a's, seven lowercase b's, five uppercase a's eight lowercase t's, and ten uppercase b's. Now I don't know if half would actually make a sentence probably not or even a word for that matter, but that's okay. So the basic thought here is we would create another dictionary called character frequency. You could do it like this or a number of different ways. And then, we would loop through all of the values, all of the items within that dictionary, within character count. We're gonna grab each key value pair. We'll check to see if key dot lower is in character frequency. If it is, then we'll simply go and get the existing value for that key that's in that dictionary and add to it that new value. So, if we had lowercase a in there already and we got to this A. Then it would come in here and it would add this five to the existing five. So that the value associated with a, that lowercase a, is now ten. If it was not in there, if that key did not exist, we would simply insert it and then assign to that key the current value that we've gone through, and then would simply print it out. So let's go ahead and see what that would look like. So we'll come over here we'll do the same thing, but instead of lists this time we'll do dictionary, and there you have it. So we have 17 instances of B. We have eight instances of T, and ten of A. And those numbers look correct to me. So these solutions to these problems are absolutely valid. They will definitely work, but there is a little bit more of an elegant way to get to that end point using comprehensions. So let's start with the list comprehension.

Back to the top