 Welcome back everyone. This is Brian. And we're going to talk about the map function. Specifically, this is looping without actually having a loop. And under the hood maps a function call to a collection of items and it looks something like this map function to whatever collection we have. So for example, we could have a list and each item that list we could pass it to the function. Let's dive in and take a look. Let's start off with some basic usage. So we're going to get a list of people. And in that list of people, Matt, Brian, Tammy, Marcus, we want to pass this to a function and count the length of each item. And we want to get another list that has those links. So we're going to cover an old way and a modern way. So let's do the old way first. So the old way would be something like counts equals and we're just going to make a blank list and then four x in people. Then we would just want to go something like counts dot append. Pretty simple. What we're doing here, we're just going to say Lynn, we want to get the length of each element. Then when we're done, we just want to print it out. So we're going to say print old way. And let's just go ahead and print that out. Pretty simple, pretty easy to understand works exactly the way you think it would. It's a little wordy. Now the modern way is going to use the map function. And we're going to shorten this. Remember when I said at the very beginning of this as code becomes more complex, what we have to write actually becomes shorter and harder to understand. This is a great example. So I'm actually going to start off with print. And then we're going to format that say mapped. I want to actually capitalize that just so it stands out. Now we're going to say list, which is literally going to convert this into a list going to list through every element. We're called the map function. And inside the map function, we're gonna call Len and people. What this looks crazy. Let's run this. Sure enough, it works as expected. So what's going on under the hood here is we're saying, okay, call print. And then we're going to list map. Now what maps going to do is it's going to take all of these people right here. And shove it into a function. In this case, we're using the built in length function or Len, which is going to get the size. So essentially, we're replacing all of this code in one line. It's very, very cool. The main takeaway from this little segment though is we're taking a collection of items and going through each and every single item and sending it to that function one at a time. We're getting back a map object. So we have to convert that to a list. Let's go deeper still into complexity land and we're going to make this a little bit more complex. We're going to combine elements. And I put some notes notice different links. And we're also passing multiple arguments. So what we're going to do here and I've got some stuff already queued up ready to paste. First names and last names, we have two tuples. And we're going to combine apple and pie chocolate and cake fudge and brownies and oh, notice how pizza does not have something we can partner it up with. We're going to examine how this works and how it behaves. We're also going to define our own function. So I'm going to say def merge and emerge we're going to have an A and a B, which is just simply going to return a plus. Some sort of space must be very simple function. That's all this needs to do. So our function call just simply needs to return something and whatever we return is going to get added to the map object. So let's go ahead and say x equal map. And we're going to use our merge function that we just wrote this guy right here. Now we're going to merge the first names and the last names. So really, when we look at this, it should actually be something like this, args. So we can have zero or more arguments, I should say one or more arguments. So that's why they do args initially. So we're going to map our merge function to these two arguments first names and last names, which are these tuples right here. Let's go ahead and print out x and see what we get back. This is where newbies often get tripped up. They go, what, what is this, we have a map object. Well, how do we work with this map object? The thing is, you don't want to work with it directly. What you're going to want to do is actually converted into something that we can work with. So we're going to say print list. And we're going to just simply list out x. What this is going to do is it's going to go into the map object and say, give me every single item and then it's going to convert it or cast it into a list that we can then use. For example, Apple space pie chocolate space cake fudge space brownies. Notice these are individual items that have been merged together. And there is no pizza. So because it didn't have anything, we could partner it up. It just failed out silently, and we don't have to worry about it crashing our program. Okay, I'm just going to say if you're a newbie, and you've skipped any of the videos, you're about to have a bad time. So we're going to do multiple functions, meaning we're going to combine functions and call multiple functions in one map call. And if you have not been following along, you are about to have a really confusing time. So I'm going to pay some functions add, subtract, multiply, divide, they're very simple. Add, adds, subtract, subtract, multiply, multiplies and divide. Well, shockingly divides. I don't want these to be complex, because I don't want to focus on the mathematics here. We are going to make another function, though. Def do all going to have func and num. func is going to be a function and num is going to be a list of numbers. And we are going to return the function call with num zero and num one. So all we're doing is we're saying get the first element of the list and get the second element of the list. So matching this up basically add num zero, num one. So it would be something like add num zero, and then num one, something like that. And then subtract, multiply, divide, so on. In production level apps, you're going to want to make sure that this actually has two elements and that they're the right data types, et cetera, and so forth. But I don't want to focus on the complexity of any of this. I just want to show you this is the basic structure we're going to use. Now we're going to make a list actually in this case a couple of functions. We're going to have add, attract, multiply and divide. And these are the functions we have pasted earlier. Remember functions in Python are opera objects so we can treat them as variables as objects. So I'm just making a tuple filled with those functions. Now I'm going to make some values and I'm going to just this can be a little confusing. We haven't done this before. We're going to put a list inside of a list and I'm going to say five comma three. If that looks confusing, it's because we have a list inside of a list. And this is the one and only element in that list. It could be something like, so you would have one, two, three, four items in this one list. So yes, you can put lists and stuff like that inside of lists. Very cool how you can do that. We haven't really touched on that complexity yet. But today is that day. Now I'm going to make our numbers and I'm going to take values and convert it into a list that is going to be multiplied. By the length of our functions. So basically I'm going to take this guy right here and say how many functions do we have one, two, three, four, add, subtract, multiply and divide. And I'm going to multiply it by that number. From there, just hit the pause button. Let's print out what we have just to make sure what we're doing actually makes some type of logical sense. So we're going to print out F those are our functions. And then let's go ahead and print out and just so we can see what this is. Save run. Okay, so F is function add function, subtract function, multiply and function divide. And you can see the memory locations of each one of those. Now we have our list of data here. This is list values time the length. And it is one, two, three, four. This is important because if you scroll up and you remember when we were combining first and last names, if it doesn't have a matching partner, it will just skip over it silently. So we want to make sure that we have a list for each and every function call. So this would be add, subtract, multiply and divide. And that's where we're getting this num zero number one. So zero in this case would be five and one would be three. Hopefully that makes sense. If not, you may need to rewatch that. I tried to explain it the best I can. But if you've been skipping videos or haven't been watching them all the way, that may make absolutely no sense. So now we're going to say m equals map. We're gonna call the map function. And we're going to map do all to f and n. What is this witchcraft? What did we just do here? So basically what we're just saying is map the do all function to our list of functions. And the data we want to put on each function is well, this big list of lists. So each function gets its own little list of data. Wow, and it's going to return back a map object. So we're just going to go ahead and print out. We want to convert that map object to a list. Go ahead save run and the result is going to be eight to 15 and 1.6. So what is going on here? Well, five and three is eight five minus three is two five times three is 15 and five divided by three is 1.6 big long number here. But this is really, really cool. We've just basically said, do a ton of functions in one little call and gave it the information each function is going to use. So super quick recap for we close this video down. The map function is very, very powerful. Once you wrap your head around it, you're going to have a function and then you have some sort of iterable and you can have multiple iterables, but they have to line up in order for them to call. For example, first names and last names, we just simply didn't have anything that lined up with pizza. So there was no like deep dish pizza or anything like that. But we had apple pie, chocolate cake, fudge brownies. I hope you enjoyed this video. You can find the source code out on github.com. If you need additional help, myself and thousands of other developers are hanging out in the void realms Facebook group. This is a large group with lots of developers and we talk about everything technology related, not just the technology that you just watched. And if you want official training, I do develop courses out on udemy.com. This is official classroom style training. If you go out there and the course you're looking for is just simply not there. Drop me a note. I'm either working on it or I will actually develop it. I will put a link down below for all three of those and as always, help me help you smash that like and subscribe button. The more popular these videos become, the more I'll create and publish out on YouTube. Thank you for watching.