Python Jumpstart by Building 10 Apps Transcripts
Chapter: App 9: Real Estate Analysis App
Lecture: Data mining with list comprehensions
0:00 All right, I said there is a better way than using these procedural loops to answer these questions. Now, check this out, let's change this here,
0:09 let's write some code that maybe is a little surprising to you unless you have seen it,
0:15 so we'll say again prices, and what we want to generate is a list of numbers, we wrote this code which generated a new list,
0:21 it's equivalent to saying list, like so and then we iteratively looped over, and down here you can see we even added a test in our loop
0:30 now there is a concept that lets us write a more declarative style of code where the implementation actually handles the looping and all that for us,
0:37 so we can come down here and write what looks like brackets, we are going to define a list, but we are going to put something different in there
0:44 we are put in an expression, first line is always going to be the projection or items to create, something like this,
0:52 so what do we want, we want the purchase.price, here, now it's a little bit backwards because you have to imagine, all right,
0:57 there is a variable p, that I am about to talk about, and it's going to be a purchase so I want the price
1:03 and then the next thing down here is going to be the set to process, for lack of a better word, so here we'll say for p in data:,
1:12 so what we want is we want to go through all the data, grab out every one of those we are going to call it this p
1:21 and we are going to pull out the price, now we could pull things like a tuple, I could say p.beds like that, and even like p.state,
1:29 no that's not going to be interesting, it's all the same state, let's say city, maybe we'll get something there and I could just print prices,
1:37 I think in this case I need some parenthesis, yes, one of the rare cases where tuples need parenthesis,
1:44 and let's just say return so it doesn't actually crash. And notice, we looped through here and here is the price,
1:50 here is the number of beds, here is the city, price, beds, price, beds city, so this is like a projection, kind of like if you are
1:59 familiar with SQL, queries and those kinds of things, that's what this is but we just want one item, which is just the prices.
2:06 So here you can see all the prices. If you want to know what type that is it's just a list, class list, that's its values.
2:15 So think about this for a minute, before what we did we had to allocate the price, loop over each item, and then do some processing,
2:22 granted this processing is very simple, it's just added to the list, so it's a little harder to see the value here
2:28 but here we can just declare expression that will build the list, and it uses the same mechanism as the list here,
2:35 let's take the same idea and apply it the more interesting scenario here, this, so here we have the same thing,
2:44 and the last part that I hadn't shown you before is the test or condition, if you are familiar with data bases this would be like the where clause,
2:51 so we could say if p.beds, so now if I run this, we should get the same answer below,
2:59 and we do exactly as we had before, but we don't need these tests, what we are really after is not just the average price of a two bedroom home,
3:08 we also want to know the average number of bathrooms and maybe the average square footage, so this can get a little more interest in here,
3:17 so we'll say the average price of a two bedroom home let me tighten this a little bit, so we'll say the average two bedroom home is
3:25 some price with a certain number of baths and a certain square footage, now I put the 0 here so just this will run,
3:30 here you can see we are still getting the same answers. Now, suppose I want to know this, well, previously with this looping mechanism,
3:36 I would have to come up with another list of let's say baths=something like this,
3:43 and down here I would have to also say bath.append pur.baths, and so on. With the conciseness of these lists here,
3:53 we could do something like this, we could just say two bed homes like this, and let's get back here like so,
4:02 and so what we'll get is a list of two bedroom homes and then down here we can say the average price is going to be the mean,
4:08 now if we try to give it two bedroom homes, remember, this was crashing, so as I don't know what to do with, purchases,
4:14 but we can go into this set and we can just write in line here and say what I would like is p.price for p in this.
4:23 And now we are back to our average, so I said look, write in this little bit of code here, we are just going to do this little projection,
4:31 now, let's get the average baths. So here I can say baths, and the average square feet. All right, now let's see what their answer is,
4:42 maybe we could round this off here, so we'll just use the round function here, so the average two bedroom home in Sacramento that was for sale
4:54 were actually sold in 2008 was sold for 165,000 dollars, it had 1.4 baths and the square footage of 957.2 square feet. How cool is that?
5:06 All right, so this concept here is called a list comprehension, and it's really cool, it has some performance benefits, and some performance drawbacks.
5:18 You will see that there is something extremely similar to it, called a generator expression,
5:23 but first let's look at the concept of list comprehensions.