1 00:00:00,000 --> 00:00:08,011 [MUSIC] 2 00:00:08,011 --> 00:00:11,858 Today, I have to introduce our first speaker. 3 00:00:11,858 --> 00:00:15,434 She is a teammate and a colleague and just a one wonderful person. 4 00:00:15,434 --> 00:00:17,644 She's out here in the Midwest for me, y'all. 5 00:00:17,644 --> 00:00:22,561 Megan Amendola has been with Treehouse over three years. 6 00:00:22,561 --> 00:00:27,124 She has a bachelor's degree in Speech Pathology and Audiology, and 7 00:00:27,124 --> 00:00:29,416 a master's degree in Education. 8 00:00:29,416 --> 00:00:34,035 After teaching for five years, she decided to make a switch, and 9 00:00:34,035 --> 00:00:37,816 to Treehouse's full stack JavaScript tech degree. 10 00:00:37,816 --> 00:00:41,891 She continued her education, learning UX design and Python, and 11 00:00:41,891 --> 00:00:45,112 shares the knowledge with our Treehouse students. 12 00:00:45,112 --> 00:00:50,398 Please welcome Megan Hello everyone, 13 00:00:50,398 --> 00:00:53,158 I hope you're having a wonderful day. 14 00:00:53,158 --> 00:00:56,080 Thank you, Toni, for that introduction. 15 00:00:56,080 --> 00:01:00,304 And we're gonna be talking today a little bit about some Python. 16 00:01:00,304 --> 00:01:07,094 If you don't have any Python knowledge, that's totally okay. 17 00:01:07,094 --> 00:01:08,028 And let's get into it. 18 00:01:08,028 --> 00:01:15,334 So I'm going to share my screen, share. 19 00:01:15,334 --> 00:01:19,929 Sorry for that little pop into the void. 20 00:01:19,929 --> 00:01:21,873 My little share screen. 21 00:01:24,377 --> 00:01:26,449 Yep, I'm one. 22 00:01:26,449 --> 00:01:27,921 I'm one. 23 00:01:27,921 --> 00:01:32,752 Oops, there we go, my bad, it's the morning [LAUGH]. 24 00:01:32,752 --> 00:01:36,817 Okay, so we're gonna be talking about levelling up your Python code with 25 00:01:36,817 --> 00:01:38,400 enumerate, map, and zip. 26 00:01:38,400 --> 00:01:45,219 These are three built-in Python functions. 27 00:01:45,219 --> 00:01:49,994 Like Tony said, I started out like a lot of our students here at Treehouse. 28 00:01:49,994 --> 00:01:51,520 I don't have a background in coding. 29 00:01:51,520 --> 00:01:56,156 My background is in speech pathology, audiology, and education. 30 00:01:56,156 --> 00:01:58,129 And I decided to make a switch. 31 00:01:58,129 --> 00:02:02,401 I took the full stack tech degree and 32 00:02:02,401 --> 00:02:07,252 fell in love with coding, and here I am. 33 00:02:07,252 --> 00:02:12,247 The reason why I picked this talk today is, I think what a lot of people 34 00:02:12,247 --> 00:02:17,246 struggle with is kind of where do I go from beginner to not a beginner, 35 00:02:17,246 --> 00:02:21,985 whatever that title is, whether it is intermediate, coder, or 36 00:02:21,985 --> 00:02:24,769 next level, whatever that title is. 37 00:02:24,769 --> 00:02:30,251 I think a lot of people have a hard time figuring out what that switch is kind of, 38 00:02:30,251 --> 00:02:34,588 where you go from super beginner to someone who knows to code. 39 00:02:34,588 --> 00:02:38,043 And in my opinion, it functions like the ones we're gonna talk about today. 40 00:02:38,043 --> 00:02:44,832 Those are the ones that kind of take you over the edge and no longer a beginner. 41 00:02:44,832 --> 00:02:49,302 These are the kinds of things that can make your code more simple. 42 00:02:49,302 --> 00:02:52,447 It can dry up your code. 43 00:02:52,447 --> 00:02:56,676 And it can just make the things that you do in the way that you problem-solve 44 00:02:56,676 --> 00:02:59,850 a little bit more advanced, a little bit more dynamic. 45 00:02:59,850 --> 00:03:03,056 So that's why we're gonna talk about those today. 46 00:03:03,056 --> 00:03:06,534 I am going to be jumping into code. 47 00:03:06,534 --> 00:03:10,570 So if you want to code along with me this is your forewarning. 48 00:03:10,570 --> 00:03:15,815 You can open up a treehouse workspaces if you're a treehouse student. 49 00:03:15,815 --> 00:03:20,160 Python is already installed in there and just create like an app.pi file. 50 00:03:20,160 --> 00:03:24,240 If you wanna work locally, open up your favorite text editor. 51 00:03:24,240 --> 00:03:29,173 And you would need to have Python installed on your computer to use 52 00:03:29,173 --> 00:03:31,514 the Python we're about to do. 53 00:03:31,514 --> 00:03:37,716 But you can open up a file in your favorite text editor to follow along. 54 00:03:37,716 --> 00:03:42,616 All the codes are already up on GitHub if you just wanna grab it as notes. 55 00:03:42,616 --> 00:03:45,965 The GitHub is, if you search under treehouse festival, 56 00:03:45,965 --> 00:03:48,633 Level up your Python Code, you'll find it. 57 00:03:48,633 --> 00:03:52,383 So let's get started. 58 00:03:52,383 --> 00:03:57,802 The first one we're gonna talk about is enumerate. 59 00:03:57,802 --> 00:04:00,935 So enumerate, what does it do? 60 00:04:00,935 --> 00:04:06,516 Very simply, it adds a counter to an iterable. 61 00:04:06,516 --> 00:04:10,273 So sometimes, you might create like a count variable, and 62 00:04:10,273 --> 00:04:12,680 then when going through a for-loop, 63 00:04:12,680 --> 00:04:18,034 you might do like count plus equals one to count through each thing in your iterable. 64 00:04:18,034 --> 00:04:20,050 And enumerate does that for us. 65 00:04:20,050 --> 00:04:21,952 You don't have to create that variable. 66 00:04:21,952 --> 00:04:24,572 And you can also set where the counter starts. 67 00:04:24,572 --> 00:04:25,494 This one, simply, 68 00:04:25,494 --> 00:04:28,598 you would use it when you need to count something while you iterate. 69 00:04:28,598 --> 00:04:33,347 So it's kind of as simple as that, that's as easy as it is. 70 00:04:33,347 --> 00:04:34,802 So we're gonna jump into some code. 71 00:04:34,802 --> 00:04:39,139 I'm gonna switch over to my text editor. 72 00:04:39,139 --> 00:04:41,114 I'm gonna move this backstage over here. 73 00:04:41,114 --> 00:04:45,191 That way, I can see just in case something's going wrong [LAUGH]. 74 00:04:45,191 --> 00:04:48,280 Cool, looks like everything's going right. 75 00:04:48,280 --> 00:04:49,682 Nope, don't want that. 76 00:04:49,682 --> 00:04:50,821 Full screen, please. 77 00:04:50,821 --> 00:04:51,703 Thank you. 78 00:04:51,703 --> 00:04:56,132 Okay, so I have a list here. 79 00:04:56,132 --> 00:04:59,696 You can really make a quick list, if you're following along, of anything. 80 00:04:59,696 --> 00:05:03,024 It really doesn't matter for what we're doing right now. 81 00:05:03,024 --> 00:05:05,152 Seems we have a list of flowers. 82 00:05:05,152 --> 00:05:08,433 And it's four flowers, pretty simple. 83 00:05:08,433 --> 00:05:12,224 For a quick review, just to make sure we're all on the same live page, 84 00:05:12,224 --> 00:05:13,846 a for-loop looks like this. 85 00:05:13,846 --> 00:05:18,130 So if we wanted to loop through our list, 86 00:05:18,130 --> 00:05:22,043 we could do, for flower in flowers. 87 00:05:22,043 --> 00:05:26,025 .And then let's just, print(flower). 88 00:05:26,025 --> 00:05:29,446 Save, and let's run it in the console. 89 00:05:29,446 --> 00:05:32,172 This is enumerate.py. 90 00:05:32,172 --> 00:05:37,456 And you can see, that's my little screen share thing, can we move that over there? 91 00:05:37,456 --> 00:05:38,018 There we go. 92 00:05:38,018 --> 00:05:40,181 I get sunflower, daisy, rose, peony. 93 00:05:40,181 --> 00:05:44,454 So it grabs each item in our iterable, print it to the screen. 94 00:05:44,454 --> 00:05:50,177 But with counting, if we wanted to count each flower, let's say we, 95 00:05:50,177 --> 00:05:55,706 this is a database of flowers and we're listing out in the console, 96 00:05:55,706 --> 00:06:02,207 a numbered list of all the flowers that we sell at our nursery, just for an idea. 97 00:06:02,207 --> 00:06:06,744 I also use counts a lot of the times with like games, counting turns, 98 00:06:06,744 --> 00:06:08,240 and things like that. 99 00:06:08,240 --> 00:06:13,593 So you might have done something previously like this, so you count = 0. 100 00:06:13,593 --> 00:06:19,132 And then you do, for flower in flowers. 101 00:06:19,132 --> 00:06:22,053 Oops, I hope I spell it correctly. 102 00:06:22,053 --> 00:06:24,533 There we go. 103 00:06:24,533 --> 00:06:27,822 And then let's do, 104 00:06:27,822 --> 00:06:32,469 print( count, flower). 105 00:06:32,469 --> 00:06:36,526 And then we would do, count += 1. 106 00:06:36,526 --> 00:06:39,326 So our count is going to start at zero and it's gonna go to one. 107 00:06:39,326 --> 00:06:43,440 And just so our console doesn't get full of stuff, 108 00:06:43,440 --> 00:06:47,564 I'm going to comment out the previous for-loop. 109 00:06:47,564 --> 00:06:51,413 That way our console just doesn't get so busy. 110 00:06:51,413 --> 00:06:55,513 Okay, so you see we get zero sunflower, one daisy, two rose, three peony. 111 00:06:55,513 --> 00:07:01,762 So it gives us a count or a number that goes with the items in our iterable. 112 00:07:01,762 --> 00:07:07,468 Now, let's see what enumerate does if we use that instead. 113 00:07:07,468 --> 00:07:12,583 So we have for, and now enumerate is going to give us two things, right? 114 00:07:12,583 --> 00:07:18,577 It's going to give us a single item from our iterable and the count number. 115 00:07:18,577 --> 00:07:22,458 So we need to unpack that as two things here. 116 00:07:22,458 --> 00:07:27,014 So I'm gonna unpack it as count, and 117 00:07:27,014 --> 00:07:33,199 then as the flower, in, and gonna do enumerate. 118 00:07:33,199 --> 00:07:39,889 And inside, we're going to pass in our iterable, which is flowers. 119 00:07:39,889 --> 00:07:43,324 I'm gonna keep typing this out, and then I'm gonna say what this is one more 120 00:07:43,324 --> 00:07:45,484 time just to make sure everyone's following. 121 00:07:45,484 --> 00:07:50,460 So, print(count, flower). 122 00:07:50,460 --> 00:07:51,753 Make sure you save them. 123 00:07:51,753 --> 00:07:56,492 Okay, so enumerate gives us, oops, there we go, 124 00:07:56,492 --> 00:07:59,482 enumerate gives us two things. 125 00:07:59,482 --> 00:08:01,659 We pass in an iterable, and 126 00:08:01,659 --> 00:08:06,887 then it pairs each item in the iterable with a number or a counter. 127 00:08:06,887 --> 00:08:11,179 And then you unpack that as two variables in your for-loop 128 00:08:11,179 --> 00:08:13,815 in order to access those two items. 129 00:08:13,815 --> 00:08:19,394 So, I'm gonna clear my console, now run it again. 130 00:08:19,394 --> 00:08:22,913 Oops, let me do this, just so it's very clear what we're running. 131 00:08:22,913 --> 00:08:25,648 I am gonna comments out what we had before. 132 00:08:29,121 --> 00:08:30,036 Here we go. 133 00:08:30,036 --> 00:08:34,017 Okay, so you see we get the exact same result, zero sunflower, 134 00:08:34,017 --> 00:08:36,318 one daisy, two rose, three peony. 135 00:08:36,318 --> 00:08:43,296 So we took one, two, three, four lines of code down to two lines of code, 136 00:08:43,296 --> 00:08:48,827 which may not seem like a ton in the grand scheme of things. 137 00:08:48,827 --> 00:08:52,749 But it is a way for you to look through your code and 138 00:08:52,749 --> 00:08:59,285 see how you can shorten things, how you can make things a little bit more simple. 139 00:08:59,285 --> 00:09:02,610 Enumerate is a little bit more complicated, 140 00:09:02,610 --> 00:09:08,216 a little bit more advanced than doing count, count += 1, in my opinion. 141 00:09:08,216 --> 00:09:12,077 So I think any potential employer looking at your code would be like, look at that, 142 00:09:12,077 --> 00:09:14,909 they're using enumerate, instead of doing a count variable. 143 00:09:14,909 --> 00:09:19,645 They obviously know Python a little bit better than maybe somebody else's 144 00:09:19,645 --> 00:09:21,307 work that they looked at. 145 00:09:21,307 --> 00:09:24,044 So that is essentially how enumerate works. 146 00:09:24,044 --> 00:09:25,087 So it's kind of simple. 147 00:09:25,087 --> 00:09:28,358 That's what it does, there's not a lot to it. 148 00:09:28,358 --> 00:09:32,833 But one thing that's very important to know about enumerate, 149 00:09:32,833 --> 00:09:37,570 we come down here, I'm gonna do enumerate passing the iterable. 150 00:09:37,570 --> 00:09:40,530 And I'm gonna try to do an access like this, 151 00:09:40,530 --> 00:09:45,409 like I'm trying to access the first item, which you think maybe it would 152 00:09:45,409 --> 00:09:50,226 give you like a tuple that has the count and the flower paired together. 153 00:09:50,226 --> 00:09:51,413 But let's see what happens. 154 00:09:51,413 --> 00:09:52,514 I'm gonna save. 155 00:09:52,514 --> 00:09:55,224 I'm also going to comment that out too, 156 00:09:55,224 --> 00:09:58,181 just to make our console a little bit clear. 157 00:09:59,800 --> 00:10:02,130 Run it again, and you can see we get an error. 158 00:10:03,260 --> 00:10:06,020 Enumerate object is not subscriptable. 159 00:10:06,020 --> 00:10:11,904 You can't access and enumerate using this method. 160 00:10:11,904 --> 00:10:16,293 You have to access that using a for loop or you have to translate it. 161 00:10:16,293 --> 00:10:18,454 So, if I were to print this. 162 00:10:18,454 --> 00:10:21,750 Whoops, from the beginning, there it goes. 163 00:10:21,750 --> 00:10:25,950 Print, save and 164 00:10:25,950 --> 00:10:29,860 you do a clear. 165 00:10:29,860 --> 00:10:30,470 Let's try this again. 166 00:10:30,470 --> 00:10:35,569 You can see if I print our enumerate call with flowers passed in, 167 00:10:35,569 --> 00:10:38,200 I get an enumerate object. 168 00:10:38,200 --> 00:10:41,260 And this is going to be the same for each of the things we're talking about today. 169 00:10:41,260 --> 00:10:44,400 The enumerate object is its own object. 170 00:10:44,400 --> 00:10:47,555 So, you can't just access it like you would a list, 171 00:10:47,555 --> 00:10:50,180 because it's no longer a list. 172 00:10:50,180 --> 00:10:53,740 So, you're probably thinking, well, how do I access it? 173 00:10:53,740 --> 00:10:58,197 Let's turn it, oops, back into a list. 174 00:11:00,394 --> 00:11:05,755 So just to recap, we have enumerate where I'm passing in flowers, 175 00:11:05,755 --> 00:11:11,693 then I'm translating that into a list and then I'm printing that result. 176 00:11:13,533 --> 00:11:17,587 And you can see, now we have those tuples where we have the count, 177 00:11:17,587 --> 00:11:20,994 plus the flower count plus flower all the way through. 178 00:11:20,994 --> 00:11:25,780 And if we wanted to Do just the first 179 00:11:25,780 --> 00:11:30,870 one, We could do that and we get that first tuple. 180 00:11:30,870 --> 00:11:32,649 So, that's another way to access your items. 181 00:11:33,690 --> 00:11:39,827 And for the last thing on enumerate, I said that you can pick where you start. 182 00:11:39,827 --> 00:11:44,442 So, that's important cuz sometimes you may wanna start at zero, sometimes you may 183 00:11:44,442 --> 00:11:47,952 wanna start at one, sometimes you may wanna start at a million. 184 00:11:47,952 --> 00:11:53,424 Account, flower and enumerate. 185 00:11:53,424 --> 00:11:55,581 Gonna pass in your iterable, and 186 00:11:55,581 --> 00:11:59,751 then you're gonna set start equal to whatever number you want. 187 00:11:59,751 --> 00:12:03,403 Let's do ten just so it's super clear that it's different. 188 00:12:03,403 --> 00:12:10,919 And print, Count, flower, save. 189 00:12:13,806 --> 00:12:16,926 And to comment that out too. 190 00:12:16,926 --> 00:12:18,701 And clear. 191 00:12:20,297 --> 00:12:21,780 And there we go. 192 00:12:21,780 --> 00:12:24,920 Sunflower, daisy, rose, starting at 10. 193 00:12:24,920 --> 00:12:31,765 So, pretty simple, but can be pretty powerful too Let me hop back over. 194 00:12:36,177 --> 00:12:37,674 Okay, so that was enumerate, 195 00:12:37,674 --> 00:12:40,420 the second one we're going to talk about today is map. 196 00:12:40,420 --> 00:12:44,607 And I don't know why every time I say map I think of the Dora the Explorer song, 197 00:12:44,607 --> 00:12:47,150 it always gets stuck in my head. 198 00:12:47,150 --> 00:12:49,070 So, what does map do? 199 00:12:49,070 --> 00:12:55,845 Map is going to apply a function to each element in an iterable. 200 00:12:55,845 --> 00:13:00,845 You can either create a function, which is really good if you're doing something 201 00:13:00,845 --> 00:13:04,626 longer for each element, it makes sense to kind of break out. 202 00:13:04,626 --> 00:13:06,599 Or you can create a Lambda function, 203 00:13:06,599 --> 00:13:10,749 which is great if you're doing something super short, and I'll show both. 204 00:13:10,749 --> 00:13:12,692 So, when would you use it? 205 00:13:12,692 --> 00:13:18,456 When you wanna do the same thing to every item in an iterable without a loop. 206 00:13:18,456 --> 00:13:23,790 Because without the loop, it can sometimes be more efficient and it can save memory. 207 00:13:25,540 --> 00:13:29,470 Cuz it's going to do one thing at a time versus saving everything, and 208 00:13:29,470 --> 00:13:32,300 then doing something to each item. 209 00:13:32,300 --> 00:13:34,050 So, we can be a little more efficient. 210 00:13:35,200 --> 00:13:38,590 So for example, well, let's jump into the examples. 211 00:13:38,590 --> 00:13:40,215 Let's jump into code. 212 00:13:40,215 --> 00:13:46,759 Escape, hop back over here. 213 00:13:46,759 --> 00:13:48,320 Clear, and let's pop over to map. 214 00:13:49,330 --> 00:13:52,240 Okay, nope, that's it. 215 00:13:52,240 --> 00:13:53,740 Here we go, map. 216 00:13:53,740 --> 00:13:56,550 So, we have our same list, same flowers list. 217 00:13:56,550 --> 00:13:59,950 I'm gonna pull up my notes over here just so I'm on the same page. 218 00:13:59,950 --> 00:14:02,230 Let me look at our time. 219 00:14:02,230 --> 00:14:04,049 I'm gonna do a little copying and pasting. 220 00:14:04,049 --> 00:14:08,156 Sorry, if you're following along, just for time. 221 00:14:08,156 --> 00:14:10,636 I wanna make sure I get through everything. 222 00:14:10,636 --> 00:14:13,114 So, I'm gonna copy this over. 223 00:14:15,167 --> 00:14:18,472 I'll talk about what I just put on the screen. 224 00:14:18,472 --> 00:14:25,269 Okay, so here we have a regular loop, I have created an empty list called plural. 225 00:14:25,269 --> 00:14:28,925 And I'm looping through our flowers, 226 00:14:28,925 --> 00:14:33,795 and if the last letter is a y, I'm removing that y and 227 00:14:33,795 --> 00:14:38,693 adding ies to pluralize, like daisy into daisies. 228 00:14:38,693 --> 00:14:42,453 And then, otherwise if it doesn't have that, we're adding s, so, 229 00:14:42,453 --> 00:14:43,938 sunflower to sunflowers. 230 00:14:43,938 --> 00:14:47,451 And then we're going to print this list at the end. 231 00:14:47,451 --> 00:14:49,420 So, that is saved. 232 00:14:49,420 --> 00:14:51,277 So, let's do python 233 00:14:56,274 --> 00:15:00,679 You can see we get sunflowers, daisies, roses and peonies. 234 00:15:00,679 --> 00:15:03,770 And this is probably something that a lot of you are familiar with. 235 00:15:03,770 --> 00:15:08,555 I feel like I do stuff like this a lot when I'm working on coding, challenges, 236 00:15:08,555 --> 00:15:10,920 projects, things like that. 237 00:15:10,920 --> 00:15:14,710 I have a for loop and then I do a bunch of stuff inside that for loop. 238 00:15:14,710 --> 00:15:19,296 So, let's take a look at what this looks like with map instead. 239 00:15:21,937 --> 00:15:23,413 Let me move this too, 240 00:15:23,413 --> 00:15:29,570 that way if you're trying to type this I can keep this on the screen here. 241 00:15:29,570 --> 00:15:34,131 Okay, so essentially, I'm going to take what's inside of our for 242 00:15:34,131 --> 00:15:37,900 loop and I'm gonna pull that out into its own function. 243 00:15:39,070 --> 00:15:43,160 So, I'm gonna just copy this over again just in case of time. 244 00:15:45,050 --> 00:15:48,256 So, map, we have pluralize passing in the word. 245 00:15:48,256 --> 00:15:52,675 If the last letter is y, add ies, if it's not, add s. 246 00:15:52,675 --> 00:15:55,599 So, exact same thing, just pulled out into a function. 247 00:16:03,191 --> 00:16:10,081 Now, plural, here, I've created a variable called plural and I've called map. 248 00:16:10,081 --> 00:16:15,010 And you pass in the function and then you pass in the Iterable. 249 00:16:15,010 --> 00:16:20,208 So, this function right here, just like we did up above with this for loop, 250 00:16:20,208 --> 00:16:25,081 is going to apply to each element in the iterable that we have passed in, 251 00:16:25,081 --> 00:16:26,647 which is our flowers. 252 00:16:28,527 --> 00:16:33,245 And now because it's a variable, you're not gonna see anything on the screen. 253 00:16:33,245 --> 00:16:37,893 So, I'm gonna do print plural, save, and 254 00:16:37,893 --> 00:16:42,301 I'm gonna comment out our for loop here. 255 00:16:45,989 --> 00:16:48,907 Okay, and let's run this? 256 00:16:54,431 --> 00:16:58,519 And you can see, like I said before, we get a map object this time, 257 00:16:58,519 --> 00:17:00,565 instead of an enumerate object. 258 00:17:00,565 --> 00:17:03,510 Because each of these is going to turn into their own object. 259 00:17:03,510 --> 00:17:08,204 So in order to view what we have going on here, 260 00:17:08,204 --> 00:17:14,410 I need to do list and translate our map object into a list. 261 00:17:14,410 --> 00:17:16,270 So, let's now run this again. 262 00:17:16,270 --> 00:17:19,604 And now you can see, we get the same list that we did from our for loop. 263 00:17:19,604 --> 00:17:21,780 We have sunflowers, daisies, roses and peonies. 264 00:17:23,750 --> 00:17:28,593 And if you take a look at the code, we have what, one, two, three, four, five, 265 00:17:28,593 --> 00:17:31,141 six, technically, seven lines of code. 266 00:17:31,141 --> 00:17:34,846 And before we had one, two, three, four, five, six, seven lines of code. 267 00:17:34,846 --> 00:17:40,228 So, it's about the same for amount of code, but again, 268 00:17:40,228 --> 00:17:44,955 I think it shows your knowledge of Python better. 269 00:17:44,955 --> 00:17:48,896 And there will be times and there may be times where things like memory and 270 00:17:48,896 --> 00:17:53,096 time are very important in your project that you might wanna use map instead of 271 00:17:53,096 --> 00:17:55,053 a for loop because it can be faster. 272 00:17:57,243 --> 00:18:03,070 Now, another thing to know about this is that you can use built in functions. 273 00:18:04,550 --> 00:18:07,510 Copy this over because I think I'm running out of time. 274 00:18:07,510 --> 00:18:09,720 I talk really fast to myself at home apparently. 275 00:18:12,050 --> 00:18:18,873 Okay, as you can use other built in functions. 276 00:18:18,873 --> 00:18:21,635 So, you can use like length here, 277 00:18:21,635 --> 00:18:27,360 the length function which usually you see it with the two parentheses. 278 00:18:27,360 --> 00:18:29,950 But when using map, you don't wanna use those parentheses. 279 00:18:31,040 --> 00:18:35,665 So, here this is going to get the length of every string inside of flowers and 280 00:18:35,665 --> 00:18:39,560 it's gonna turn it into a list and we're gonna see that list. 281 00:18:39,560 --> 00:18:40,600 So, let's check that out. 282 00:18:42,130 --> 00:18:46,222 Oops, I forgot to save, I hope you probably saved first. 283 00:18:46,222 --> 00:18:48,222 Here we go, so we get 9, 5, 4, 5. 284 00:18:48,222 --> 00:18:54,490 So, any already built in functions like length, you can also use with map. 285 00:18:54,490 --> 00:18:56,340 You just don't wanna use the parentheses. 286 00:18:57,450 --> 00:19:02,690 And the last thing I wanted to show was a Lambda function. 287 00:19:04,210 --> 00:19:06,259 And again, if you're following and you're like, my God, 288 00:19:06,259 --> 00:19:07,930 she's copy pasting now and I can't keep up. 289 00:19:07,930 --> 00:19:10,336 All of this is already saved on a GitHub repo, so 290 00:19:10,336 --> 00:19:14,140 you will have all the code that we have that I'm doing right now available. 291 00:19:16,250 --> 00:19:18,050 So, here we're doing a Lambda function. 292 00:19:19,320 --> 00:19:23,790 I created a list of numbers and then I'm doubling them. 293 00:19:24,890 --> 00:19:28,045 So, Lambda function, you use the Lambda keyword, 294 00:19:28,045 --> 00:19:33,210 and then just like a function, this is the parameter I'm passing in. 295 00:19:33,210 --> 00:19:38,180 The parentheses here is saying, now I'm starting what's inside of my function, 296 00:19:38,180 --> 00:19:42,760 which is I'm going to take that parameter that I passed in this number. 297 00:19:42,760 --> 00:19:50,003 I'm gonna multiply it by two, and I'm gonna do all of that on this iterable. 298 00:19:50,003 --> 00:19:54,339 So, this is our function, which is just taking the number, 299 00:19:54,339 --> 00:19:58,431 each number and multiplying it by two for this iterable. 300 00:19:58,431 --> 00:20:00,880 And I'm gonna turn that into a list and then I'm printing it. 301 00:20:00,880 --> 00:20:01,780 So, let's take a look. 302 00:20:07,113 --> 00:20:11,037 And I didn't do the one before, so this is the one we're looking at. 303 00:20:11,037 --> 00:20:15,251 So, 1 times 2 is 2, 2 times 2 is 4, etc. 304 00:20:15,251 --> 00:20:20,189 So, map can be super helpful, can allow you to do things a little bit quicker and 305 00:20:20,189 --> 00:20:21,981 save a little bit of memory. 306 00:20:23,286 --> 00:20:27,638 Close this, and we pop back over to map. 307 00:20:30,014 --> 00:20:33,431 And the last one we're going to do is zip. 308 00:20:33,431 --> 00:20:36,516 So zip returns an iterator of tuples. 309 00:20:36,516 --> 00:20:40,418 So it's going to aggregate iterables into tuples. 310 00:20:40,418 --> 00:20:45,436 And very, very important, it works until the shortest iterable is exhausted. 311 00:20:45,436 --> 00:20:50,670 So, if you have two different sizes of lists, say one has seven things and 312 00:20:50,670 --> 00:20:55,756 one has five, it's only going to work until it exhausts the short one. 313 00:20:55,756 --> 00:20:58,259 And then why would you use it? 314 00:20:58,259 --> 00:21:01,537 Whenever you wanna iterate over more than one iterable at the same time, 315 00:21:01,537 --> 00:21:03,269 that's what zip's gonna help with. 316 00:21:03,269 --> 00:21:04,436 So let's take a look. 317 00:21:07,577 --> 00:21:10,681 So here you can see I created a second list. 318 00:21:10,681 --> 00:21:15,213 These are of trees, and I have one, two, three, four. 319 00:21:15,213 --> 00:21:18,397 I have four and four, so same number in each. 320 00:21:18,397 --> 00:21:20,776 And same, I'm gonna do a little copy and pasting to save time. 321 00:21:20,776 --> 00:21:25,030 My apologies for anyone who's following along. 322 00:21:27,669 --> 00:21:30,961 Okay, so zip, I just jump in right to it. 323 00:21:30,961 --> 00:21:34,573 You pass in all of the iterables that you wanna zip together. 324 00:21:34,573 --> 00:21:37,847 It's kinda like zipping a file on your computer. 325 00:21:37,847 --> 00:21:39,917 And I'm saving that to a variable called plants. 326 00:21:39,917 --> 00:21:42,985 And I'm gonna print both plants and translate it into a list so 327 00:21:42,985 --> 00:21:44,175 we can see it both ways. 328 00:21:48,794 --> 00:21:52,433 I can see a zip object, but if I translate it into a list, 329 00:21:52,433 --> 00:21:57,943 I can see the tuples where it's paired all of the elements of our two lists together. 330 00:22:00,015 --> 00:22:04,855 If I wanted to loop through the items that I have now, 331 00:22:10,043 --> 00:22:15,492 I can do for plant in zip, and I get flowers, pass in flowers and trees. 332 00:22:15,492 --> 00:22:20,668 And print plant, I'm going to get each individual tuple. 333 00:22:20,668 --> 00:22:25,368 But instead I can do tree, flower and 334 00:22:25,368 --> 00:22:32,810 break it back out into each of those tree, flower, save. 335 00:22:33,963 --> 00:22:40,478 Here, and you can see instead I get the pairings of the trees and flowers. 336 00:22:40,478 --> 00:22:42,948 And I did a mistake and I'm glad I did it. 337 00:22:42,948 --> 00:22:47,568 Whichever order you pass in here needs to match the order here. 338 00:22:47,568 --> 00:22:52,530 Cuz you can see I have tree, flower, but I really have flower, tree. 339 00:22:52,530 --> 00:22:57,374 So that can get a little mixed up and a little confusing. 340 00:22:57,374 --> 00:23:03,798 So this actually needs to be flower, tree. 341 00:23:14,609 --> 00:23:20,335 And then, of course, if I were to delete, let's say, pear, 342 00:23:21,450 --> 00:23:26,388 Save, and if I were to run this again, you can see I only get three 343 00:23:26,388 --> 00:23:31,534 pairings out now instead of four because it ignores this last one. 344 00:23:31,534 --> 00:23:36,169 Because it only goes until one iterable is exhausted. 345 00:23:36,169 --> 00:23:37,764 So it's very important to know. 346 00:23:40,156 --> 00:23:41,982 And I believe that it's about time. 347 00:23:41,982 --> 00:23:44,315 Sorry, I kinda ran through that last one really fast. 348 00:23:44,315 --> 00:23:47,070 The GitHub link is open. 349 00:23:47,070 --> 00:23:48,469 Let me pop this back up. 350 00:23:51,097 --> 00:23:54,858 So, that is the link to the GitHub repo, you can see it right there at the bottom. 351 00:23:54,858 --> 00:23:59,353 If you search Treehouse Festival, level up your code, you will find it. 352 00:23:59,353 --> 00:24:02,203 And it's under my GitHub, which is MissMeg. 353 00:24:02,203 --> 00:24:04,322 If you find it under there, but thank you all so much. 354 00:24:04,322 --> 00:24:06,412 I'm sorry I went so fast at the end. 355 00:24:06,412 --> 00:24:09,744 [LAUGH] But check out the repo, it has all the notes from today. 356 00:24:09,744 --> 00:24:14,231 And keep coding, keep looking up your Python knowledge, keep leveling it up and 357 00:24:14,231 --> 00:24:16,281 reach out if you have any questions.