1 00:00:00,830 --> 00:00:03,320 Raise your hand if you've ever had this happen to you. 2 00:00:03,320 --> 00:00:04,420 You have a list of things, and 3 00:00:04,420 --> 00:00:07,380 you need apply some sort of transform to all of them. 4 00:00:07,380 --> 00:00:10,210 Maybe you need to apply sales prices to a bunch of items, or 5 00:00:10,210 --> 00:00:12,970 maybe you need to update an attribute in a bunch of objects. 6 00:00:12,970 --> 00:00:13,691 What did you do? 7 00:00:13,691 --> 00:00:17,435 Maybe you made a new list, then a for loop going over the old list, and 8 00:00:17,435 --> 00:00:21,451 in the loop run the function on each item before pending it to the new list. 9 00:00:21,451 --> 00:00:22,851 Yeah me too. 10 00:00:22,851 --> 00:00:25,052 That's where map comes into play. 11 00:00:25,052 --> 00:00:28,609 Map lets us apply a function to every member of an iterable and 12 00:00:28,609 --> 00:00:30,791 gives us back a new custom iterable. 13 00:00:30,791 --> 00:00:34,772 In Python 2, map gave back a list but that's no longer the case. 14 00:00:34,772 --> 00:00:37,330 So we'll have to cast it to a list if that's what we want. 15 00:00:37,330 --> 00:00:39,530 Let's check it out in Workspaces. 16 00:00:39,530 --> 00:00:44,570 Before we can show what map does we have to write a little function. 17 00:00:44,570 --> 00:00:49,810 So map applies a function to every item that's in iterable. 18 00:00:49,810 --> 00:00:53,700 Everything that's inside of an iterable, map applies to. 19 00:00:53,700 --> 00:00:58,891 So, for instance, we could do A little thing like this. 20 00:00:58,891 --> 00:01:07,311 We have a, let's do def double n return, n * 2. 21 00:01:07,311 --> 00:01:12,215 All right, so we're gonna apply the function 22 00:01:12,215 --> 00:01:15,950 double to our interval which is a. 23 00:01:15,950 --> 00:01:21,790 Then we're gonna print that out and we get 2, 4, 6. 24 00:01:21,790 --> 00:01:22,780 So, that's what map does. 25 00:01:22,780 --> 00:01:24,730 It's just super, super duper simple. 26 00:01:25,740 --> 00:01:31,070 That's not a super, wonderful, and handy example [LAUGH] though is it? 27 00:01:31,070 --> 00:01:33,411 So let's do a sales price. 28 00:01:33,411 --> 00:01:38,832 So let's say that our books, when we get a sales price it's always a 20% discount. 29 00:01:38,832 --> 00:01:42,637 And we wanna write a function that will take any book that we've got and 30 00:01:42,637 --> 00:01:46,620 gives back the book, but with the price as the sales price. 31 00:01:46,620 --> 00:01:51,630 So, before we can do that though, we need to come up here and 32 00:01:51,630 --> 00:01:54,260 say from copy import copy. 33 00:01:55,490 --> 00:01:59,960 Copy let's us make copies and our books are mutable. 34 00:01:59,960 --> 00:02:00,930 So, we need to make sure and 35 00:02:00,930 --> 00:02:03,440 make a copy of them inside of our functions that do things. 36 00:02:05,030 --> 00:02:08,162 So, just to be safe, that's what we want to do. 37 00:02:08,162 --> 00:02:12,140 All right, so let's say sales price, and it's gonna take a book. 38 00:02:12,140 --> 00:02:17,680 And we'll say, apply a 20% discount to the book's 39 00:02:17,680 --> 00:02:20,530 price, just so we remember what we're doing. 40 00:02:20,530 --> 00:02:24,440 So our book is gonna be a copy of our book. 41 00:02:24,440 --> 00:02:26,721 So whatever book comes in, we want to make a copy of that book. 42 00:02:26,721 --> 00:02:30,541 We don't want to modify the original book that's in the JSON file. 43 00:02:30,541 --> 00:02:33,601 Not that we would change the JSON file, but we have it loaded to memory, 44 00:02:33,601 --> 00:02:35,042 we don't wanna change all that. 45 00:02:35,042 --> 00:02:39,511 All right, so book.price should equal, 46 00:02:39,511 --> 00:02:46,220 we're gonna round this, book.price-book.price*.2. 47 00:02:46,220 --> 00:02:47,961 So that'll give us to 20%. 48 00:02:47,961 --> 00:02:52,754 And since here in the US at least, our prices always have just two digits after 49 00:02:52,754 --> 00:02:56,322 the decimal point, we're gonna round it to two digits. 50 00:02:56,322 --> 00:03:00,301 And then we're going to return the book okay? 51 00:03:00,301 --> 00:03:01,281 Cool. 52 00:03:01,281 --> 00:03:04,461 So now, let's get our sales books. 53 00:03:04,461 --> 00:03:10,400 And we're gonna call map on sales price on books. 54 00:03:11,790 --> 00:03:16,400 And we want to print, let's see, this has to be a list for 55 00:03:16,400 --> 00:03:20,810 us to be able to get to objects that are in it. 56 00:03:22,200 --> 00:03:25,480 Let's do sales_books[0].price. 57 00:03:25,480 --> 00:03:29,472 And then you know what? Let's do print(BOOKS(0).price). 58 00:03:29,472 --> 00:03:31,560 Those should be the same book. 59 00:03:31,560 --> 00:03:34,421 We're not sorting or anything like that, so they should be the exact same book. 60 00:03:34,421 --> 00:03:36,101 So let's. 61 00:03:38,321 --> 00:03:39,001 Cool! 62 00:03:39,001 --> 00:03:44,052 So if we take our original book, which was \$13.55, 63 00:03:44,052 --> 00:03:49,221 and we put our sales price on it, we get back \$10.84. 64 00:03:49,221 --> 00:03:50,461 So pretty cool. 65 00:03:50,461 --> 00:03:52,310 Pretty handy way of doing that. 66 00:03:53,390 --> 00:03:59,240 If you've watched the comprehensions workshop, you might be thinking, wow 67 00:03:59,240 --> 00:04:04,560 that looks a whole lot like a list comprehension and you'd be right. 68 00:04:04,560 --> 00:04:07,850 What map does, is effectively a list comprehension. 69 00:04:07,850 --> 00:04:10,350 So, let's write this as a list comprehension. 70 00:04:11,570 --> 00:04:19,250 So, sales_books2, and we're gonna call sales_price(book) for book, and in BOOKS. 71 00:04:20,400 --> 00:04:23,990 If you haven't seen a list comprehension before, 72 00:04:23,990 --> 00:04:28,370 what we're doing here is we're making a list using a for loop. 73 00:04:30,260 --> 00:04:39,200 And so what we're saying is, put this into a list for every step in this formula. 74 00:04:39,200 --> 00:04:43,430 So we're getting a book for each book that's in BOOKS. 75 00:04:43,430 --> 00:04:46,610 And we're sending that book to sales_price. 76 00:04:46,610 --> 00:04:50,180 And so that sends us back our new and updated book. 77 00:04:51,320 --> 00:04:55,430 So if we run this again, we should get the same data and we do. 78 00:04:55,430 --> 00:04:56,980 Because we're doing the same work. 79 00:04:56,980 --> 00:04:59,650 Both of these are doing the exact same thing. 80 00:04:59,650 --> 00:05:01,360 There just doing it two slightly different ways. 81 00:05:01,360 --> 00:05:05,720 This one is using map, whereas this one is using a list comprehension. 82 00:05:05,720 --> 00:05:09,540 Now, which one of these that you choose is kind of up to you. 83 00:05:09,540 --> 00:05:12,260 They both do the same work, they both take about the same amount of time, 84 00:05:12,260 --> 00:05:14,790 they both use about the same amount of processing power. 85 00:05:14,790 --> 00:05:17,550 So, why would you pick one over the other? 86 00:05:17,550 --> 00:05:20,891 Well, map is really, really, really nestable. 87 00:05:20,891 --> 00:05:25,287 We're gonna get to this at the end of the course but you'll going to be able to see, 88 00:05:25,287 --> 00:05:30,010 you can stick map around other functionalities that we're gonna do. 89 00:05:30,010 --> 00:05:33,880 And you can't always do that easily with a list comprehension. 90 00:05:33,880 --> 00:05:39,390 List comprehensions that are more than say a line long, more than two lines long for 91 00:05:39,390 --> 00:05:43,500 sure, become really hard to read, and really hard to follow. 92 00:05:43,500 --> 00:05:48,180 List comprehensions that have more than one for in them or multiple ifs or 93 00:05:48,180 --> 00:05:48,860 something like that in it, 94 00:05:48,860 --> 00:05:53,450 they become very very difficult to track what's going on. 95 00:05:53,450 --> 00:05:58,130 Whereas map and the other fun things that we're gonna deal with in this course, 96 00:05:58,130 --> 00:06:02,590 like filter and reduce and stuff like that, are much friendlier to read and 97 00:06:02,590 --> 00:06:05,240 figure out, even in a larger scale. 98 00:06:05,240 --> 00:06:09,080 So if you're doing something just once, you just need to do this little tiny thing 99 00:06:09,080 --> 00:06:13,280 and just you're done, list comprehension's probably what you wanna use. 100 00:06:13,280 --> 00:06:18,080 If you're going for something that scales up to a large amount of lines of code, 101 00:06:18,080 --> 00:06:20,910 or maybe it needs to be callable from other places or 102 00:06:20,910 --> 00:06:24,500 whatever, then the map is probably your better bet. 103 00:06:24,500 --> 00:06:27,540 Map is a really useful utility when you need to do some work 104 00:06:27,540 --> 00:06:29,380 on a lot of items at once. 105 00:06:29,380 --> 00:06:31,920 Usually you'll get the most benefit from map when you combine it with 106 00:06:31,920 --> 00:06:34,910 other functions, like the next one we're gonna talk about, filter.