Unlimited Plugins, WordPress themes, videos & courses! Unlimited asset downloads! From $16.50/m
Data python 3
  • Overview
  • Transcript

2.4 Map

After you know a few different ways of creating collections within Python, the next logical step is to work towards modifying the data or even creating new collections from existing ones. In this lesson, I'll show you how the map function makes these operations much easier.

2.4 Map

To this point, I hope you've been getting comfortable with the thought of us continuing to work with collections of data, because in the world of software engineering, you're gonna find yourself doing a lot of that. You're gonna find yourself dealing with a lot of collections of data that you're gonna have to process, and do certain operations on. And so some of these really nice built in features of Python are really gonna help you in that vein. So in the continuation of that thought, let's continue to evolve the way that we're going to work with these collections of data. So let's say we have written some sort of function. So we're gonna start by writing a simple function and it's going to be really sweet. So we're gonna call it sweet, you've worked really hard on this function and it's gonna be really useful to you now and into the future. It's gonna take in one value, we'll just name that value x, and then it's going to do some really awesome operation. And that operation is going to be, it's gonna take that x, it's going to multiply it by 2, and then it's going to raise it to the second power or square it. So this is a really sweet function that you've been working on and so now you can reuse this when you're dealing with collections of data. So now you wanna use this and process a long collection or a list or some sort of data structure and apply this function to all of the values in that collection. So how would you do that? Well, as you've been learning, we can easily loop through these things. We could say for n in and we'll just say for the range up to 5 exclusive or something like that. And within there we could simply print out the result of applying our sweet function to that number n. And that should more than likely work. So what we'll do is we'll pop over here to our terminal and we'll go ahead and we'll run this guy. We'll say python3 map.py, and as you can see here, we get 0 up to 64, which is applying our sweet function, which is returning the xx2 squared value, and that absolutely works. But one other thing that we've been trying to do is get to a point where you start to see that there's ways to condense the code that you're writing, to make it a little bit more shorthand, to conserve space and really to think about it. If you're familiar with the concept of functional programming, we're starting to get to a point where our applications, our programs that we're writing are starting to look more functional in nature. And if you're not sure what that is and you don't have any experience with it, don't worry about it. We're not gonna focus too much on that in this course, but just to kind of put that thought in the back of your mind. So when you come back to maybe think about or check out some functional programming in the future, then you'll say, I already kind of understand some of the concepts of that. So what I'd like to do now is I'd like to adjust this where we can kind of forget about this whole for loop thing and just apply our special sweet function to a range or a collection of data without having to use this for loop. And there's some cool built-in features of the Python language that are going to allow you to do that. And as you can tell by the name of this file, map.py, there's a special function within Python called map. So let's take a look at how we can use that in this case. So what map is going to allow us to do, is going to allow us to pass in a function that we've already written, so in this case the sweet function. And it's going to allow us to also pass in a collection of data that it can iterate through and apply this function to and and then give you an output. So let's see what that would look like. We would say map, which is our function, then we would specify our custom function that we wrote, which is sweet. And then we would also give it our collection of data, which is range up through 5 exclusive, and that's it. Now if we want to be able to print this data out or show it, we're obviously going to need to do a print. Now I'm going to give you a little bit of a disclaimer. This isn't going to work exactly like you think it's going to, but I'm going to show you nonetheless. Let's go ahead and save this, and we'll switch back over to our terminal here. Let's go ahead and run this. And you're gonna see we still get the original output from our for loop, 0 up through 64, and then we have this strange little map object here. And the reason we get that is because the map function actually returns an object. And that object contains the result of the operation, but it's not just gonna print it out nicely for you. We're gonna have to do something with that data in order to show it. And typically one of the easiest things for us to do is to simply list that data. So we can apply our list function here which is gonna generate a list from that object. So let's go ahead and save that, and then we'll go back over and go ahead and run this again. But as you see here, we get the same output, 0 through 64, using the for loop and 0 through 64 using the map function. Now why should you care? Well, like I said before, in the world of functional programming, we typically avoid things like loops and it's much more using functions and generating functions and returning values from one function into another. So you start to get away from this kind of iterative process of going line by line and going through for loops and control structures and things like that, and you start to use functions to replace those things. So it's just a very nice built-in feature of the Python language that you should definitely keep in your tool belt so that in the future, if you need to apply a particular operation or a function of some sort that you've written to a collection of data, map is definitely going to be a good friend of yours.

Back to the top