Map6:35 with Kenneth Love
More than just a friend of Dora or a way to find things, `map()` lets us apply transformations to each item in an iterable.
map() takes a function and an iterable. The function should take a single argument. This function will be applied, in order, to each item in the iterable and the result of that function will be returned to
map(). In the end,
map() will return a new iterable with the mutated values.
[func(item) for item in iterable] achieves the same result, plus turns the results into a list. For simple, single-serving applications, this is often a better choice since it's often more readable at a glance.
Watch our comprehensions workshop if you want more information.
Raise your hand if you've ever had this happen to you. 0:00 You have a list of things, and 0:03 you need apply some sort of transform to all of them. 0:04 Maybe you need to apply sales prices to a bunch of items, or 0:07 maybe you need to update an attribute in a bunch of objects. 0:10 What did you do? 0:12 Maybe you made a new list, then a for loop going over the old list, and 0:13 in the loop run the function on each item before pending it to the new list. 0:17 Yeah me too. 0:21 That's where map comes into play. 0:22 Map lets us apply a function to every member of an iterable and 0:25 gives us back a new custom iterable. 0:28 In Python 2, map gave back a list but that's no longer the case. 0:30 So we'll have to cast it to a list if that's what we want. 0:34 Let's check it out in Workspaces. 0:37 Before we can show what map does we have to write a little function. 0:39 So map applies a function to every item that's in iterable. 0:44 Everything that's inside of an iterable, map applies to. 0:49 So, for instance, we could do A little thing like this. 0:53 We have a, let's do def double n return, n * 2. 0:58 All right, so we're gonna apply the function 1:07 double to our interval which is a. 1:12 Then we're gonna print that out and we get 2, 4, 6. 1:15 So, that's what map does. 1:21 It's just super, super duper simple. 1:22 That's not a super, wonderful, and handy example [LAUGH] though is it? 1:25 So let's do a sales price. 1:31 So let's say that our books, when we get a sales price it's always a 20% discount. 1:33 And we wanna write a function that will take any book that we've got and 1:38 gives back the book, but with the price as the sales price. 1:42 So, before we can do that though, we need to come up here and 1:46 say from copy import copy. 1:51 Copy let's us make copies and our books are mutable. 1:55 So, we need to make sure and 1:59 make a copy of them inside of our functions that do things. 2:00 So, just to be safe, that's what we want to do. 2:05 All right, so let's say sales price, and it's gonna take a book. 2:08 And we'll say, apply a 20% discount to the book's 2:12 price, just so we remember what we're doing. 2:17 So our book is gonna be a copy of our book. 2:20 So whatever book comes in, we want to make a copy of that book. 2:24 We don't want to modify the original book that's in the JSON file. 2:26 Not that we would change the JSON file, but we have it loaded to memory, 2:30 we don't wanna change all that. 2:33 All right, so book.price should equal, 2:35 we're gonna round this, book.price-book.price*.2. 2:39 So that'll give us to 20%. 2:46 And since here in the US at least, our prices always have just two digits after 2:47 the decimal point, we're gonna round it to two digits. 2:52 And then we're going to return the book okay? 2:56 Cool. 3:00 So now, let's get our sales books. 3:01 And we're gonna call map on sales price on books. 3:04 And we want to print, let's see, this has to be a list for 3:11 us to be able to get to objects that are in it. 3:16 Let's do sales_books.price. 3:22 And then you know what? Let's do print(BOOKS(0).price). 3:25 Those should be the same book. 3:29 We're not sorting or anything like that, so they should be the exact same book. 3:31 So let's. 3:34 Cool! 3:38 So if we take our original book, which was $13.55, 3:39 and we put our sales price on it, we get back $10.84. 3:44 So pretty cool. 3:49 Pretty handy way of doing that. 3:50 If you've watched the comprehensions workshop, you might be thinking, wow 3:53 that looks a whole lot like a list comprehension and you'd be right. 3:59 What map does, is effectively a list comprehension. 4:04 So, let's write this as a list comprehension. 4:07 So, sales_books2, and we're gonna call sales_price(book) for book, and in BOOKS. 4:11 If you haven't seen a list comprehension before, 4:20 what we're doing here is we're making a list using a for loop. 4:23 And so what we're saying is, put this into a list for every step in this formula. 4:30 So we're getting a book for each book that's in BOOKS. 4:39 And we're sending that book to sales_price. 4:43 And so that sends us back our new and updated book. 4:46 So if we run this again, we should get the same data and we do. 4:51 Because we're doing the same work. 4:55 Both of these are doing the exact same thing. 4:56 There just doing it two slightly different ways. 4:59 This one is using map, whereas this one is using a list comprehension. 5:01 Now, which one of these that you choose is kind of up to you. 5:05 They both do the same work, they both take about the same amount of time, 5:09 they both use about the same amount of processing power. 5:12 So, why would you pick one over the other? 5:14 Well, map is really, really, really nestable. 5:17 We're gonna get to this at the end of the course but you'll going to be able to see, 5:20 you can stick map around other functionalities that we're gonna do. 5:25 And you can't always do that easily with a list comprehension. 5:30 List comprehensions that are more than say a line long, more than two lines long for 5:33 sure, become really hard to read, and really hard to follow. 5:39 List comprehensions that have more than one for in them or multiple ifs or 5:43 something like that in it, 5:48 they become very very difficult to track what's going on. 5:48 Whereas map and the other fun things that we're gonna deal with in this course, 5:53 like filter and reduce and stuff like that, are much friendlier to read and 5:58 figure out, even in a larger scale. 6:02 So if you're doing something just once, you just need to do this little tiny thing 6:05 and just you're done, list comprehension's probably what you wanna use. 6:09 If you're going for something that scales up to a large amount of lines of code, 6:13 or maybe it needs to be callable from other places or 6:18 whatever, then the map is probably your better bet. 6:20 Map is a really useful utility when you need to do some work 6:24 on a lot of items at once. 6:27 Usually you'll get the most benefit from map when you combine it with 6:29 other functions, like the next one we're gonna talk about, filter. 6:31
You need to sign up for Treehouse in order to download course files.Sign up