Python Jumpstart by Building 10 Apps Transcripts
Chapter: App 9: Real Estate Analysis App
Lecture: Finding the most expensive house via lambda expressions
0:00 Ok so we've gotten our data out of the comma separated value file, converted it into our purchase class
0:07 and we've got it a list of those and we are passing it to the query data method, now in this method we are supposed to answer 4 basic questions,
0:15 what is the most expensive house, what is the least expensive house, what is the average price of a house
0:22 and what is the average price of a two bedroom house. So some of these are really easy to answer, some of them take a little bit more work.
0:29 So if we say data '.' PyCharm knows it's been able to detect that we are sending a list and it's retracted back from the load data
0:38 back to the main method and down into here and says I know I know that you are sending a list but it's not quite sure what type is on the inside,
0:46 notice when I go and pull it out I get nothing, we can actually give PyCharm a type hint, if we want and it doesn't affect the way it runs
0:56 but will give us some help and we can say this is a list of purchase objects, now if I say data '.' again we get our list stuff
1:06 but if I go to individual item you can see we have our baths, beds and so on. So that might make things a little bit easier, ok,
1:11 so the question is what is the most expensive house, what is the least expensive house, now if I could tell you that data
1:20 so we can say if data was sorted by price, it would be really easy to get the most expensive and least expensive purchase
1:28 so we could say high_purchase = and this is a list, remember, if it was sorted form low to high, high purchase would be the last one,
1:38 so we can just say minus 1, this is a beautiful thing about Python, and let me move this over like so, and the low purchase would just be data zero.
1:48 Again, we are assuming there is some data this would crash if data is empty, but how do we sort it, well, that's super easy,
1:56 we could go over here and we could say data there is two ways we can do this, we can do one which will modify the data list
2:04 and one which would return a copy that is sorted but it doesn't affect data itself. Let's assume we just want to sort this one in place
2:12 so then we can say sort notice, we can specify a key, so we can say this, we can say the key I want you to sort by is something,
2:22 now let me write something that might seem a little weird here to you, say def get_price(). And we'll give it a purchase,
2:29 so if we are given a purchase we could say return p.price. So if I go down here and say get_price and I don't call it but I just say the method name,
2:39 when it's time to sort, when it's time to get the key to sort on, the list here will actually call this function on each element,
2:46 that function will return the price of whatever the list passes to it and then it will do a sort on that new subset of data that projection of data.
2:55 So let's see if this is going to work, let's just do a print of high price.price, and down here we'll do a print, we'll do a better print in a moment,
3:04 but say low price.price. Remember, this should be like 800,000ish, this should be 1,500. Let's find out.
3:12 Well, it looks like my cool little trick to get the type hints here isn't working so well, let's take that away again.
3:20 Excellent so you can see we've got the high priced house, and the low priced house and all we had to do is sort our list.
3:28 However, this is not the coolest thing to write this function here so that we can pass it off to the key method,
3:34 wouldn't it be better if we could just put a little bit of code here that talks about how to get the price from a purchase,
3:42 well, luckily, Python can do that, we can go over here and say key, instead of giving an existing method that we have written
3:49 either a class method or just a regular method, we can just say I am going to create something called a lambda
3:55 and a lambda is like a little inline method, and so we'll say given a purchase I would like to return the price, that's it,
4:02 and we go over here and we can comment this out, we don't need that anymore, and let's try to run it and see if we still get the same information.
4:09 Perfect, so anytime you have to pass a function around and by the way how cool is that you can pass functions around,
4:15 anytime you have to pass a function around, if the function is sufficiently small we can just say
4:20 here is our little small function an inline function, lambda, this is the argument, there can be mini arguments like p, u, x, whatever, right,
4:29 given this argument which is a purchase that's because the data list contains purchases given this we are going to go and return some value
4:38 that will then be used to sort, so we are going to return a price, and there is kind of an implicit return here, right,
4:43 you don't say return, you just say there is kind of an expression, this piece of data goes to that value.
4:48 So let's just like make this output a little bit nicer here, we'll say print the most expensive house is so here we've cleaned it up,
5:00 the most expensive house is 884,000 dollars and the least expensive house is 1,551 maybe we want to say little more information about it,
5:10 like with some number of bedrooms and some number of baths, because we are using our class this is super easy we just say beds baths,
5:22 4 beds, 3 baths, we can do something very similar, for the least expensive. What do you guys think, is that a good deal,
5:32 1,500 dollars for a 3 bedroom 3 bath house, I have no idea what happened there, let's take a moment and look at this core concept of lambda expressions
5:40 and then we'll go on to answer the more interesting question that involve little bit more math.