Functional Programming in Python: The "map()" Function

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey guys this is Dan Bader and this is the third part of my functional programming primitives in Python series so in the previous two tutorials I gave you an intro to functional programming in Python what is a functional programming all about how can you represent data structures in in Python so that they're well suited for functional programming how can you define immutable data structures in an in a previous tutorial we talked about the filter function and how you can use it to filter down iterables or lists of items to you know to a smaller output list based on some arbitrary criteria some all very interesting stuff for nerds like myself and hopefully yourself so in this video I'm gonna introduce you to the map function in Python so again that's another built-in function that's available in Python you can just go map I'm on Python 3 here python 2 some of the stuff might be slightly different you're usually gonna get a list object a list object back instead of a an iterable or an iterator so this may be something you'll have to adjust for but it shouldn't be showing me too hard so let's take a look at the doc string here so the map function returns an iterator that computes the function we pass in this bit here func using arguments from each of the iterables so basically we're passing in an iterable like a list of something and in map is gonna apply our function and on each item in this list and it's gonna create an output stream or an output iterator of items that are the results of this function func called on one of the items and we're stopping once once we're done here and actually you can I think you can pass like a number of iterables here and it will all kind of be chained together we're not gonna be doing that in this tutorial so okay so I know how this stuff sounds super confusing right the first time I read this I was like ok I have no idea what this will be good for and when I would ever use this and yeah I know make me feel pretty stupid so what I'm gonna do next is I'm gonna try and make sure you get some real examples of how how this would work house would be useful so I just wanted to show you this this dataset that we used in previous installments of this series so we have this data set of scientists these are all named tuple objects so that they're immutable and also the whole thing is actually stored inside a tuple so that the whole structure is immutable so that's why I didn't use a plain list for example and by immutable it just means you know we can't reach in and modify these objects if we wanted to modify one we have to make a copy and that way we can trace these changes and we can make sure that this list is always going to be the same so immutability is kind of not having these side effects it's kind of a big big part of functional programming yeah this is the data set we're working with and what we're gonna do now is we're gonna use the map function to do some interesting stuff with it so typically you want to use the map function if you want to take and something like this like this list here and you want to transform it into a different list so basically all it does it you know takes a list of stuff applies a function to each item and then assembles a new list based on that so what we can do here I thought you know it's always a little bit hard to come up with good examples but what I thought would be fun to do here if we would actually take this list here and we would assemble a new list that contained the names and ages of these scientists so you know we're gonna ignore most of this these fields here and we're gonna focus on the name and then some calculated property that's that's based on the the born field and the birth date and I thought a good way to do this would be to use the map function so here again we're gonna use the tuple function first to convert the output of the map function into a tuple and I'm going to show you what it looks like without the tuple call but just so you get a proper proper output that's actually more interesting and then what we're gonna do we're gonna define this function that we want to map so here I'll do it with the lambda which is just a one-line function in Python that takes has a single so it can take arguments and then it can it has a single expression here and it will just automatically return the the result of that expression if you're wondering how lambdas work what they're good for I have a written tutorial on them on my website at debater org and I'm also going to put a link in the description so you can check that out alright so what we're gonna do here so in this case I'm just gonna define a dictionary object for lack of a better idea and we're gonna plug in the name and we're also gonna create this new field here called age and then I'll just calculate the age based on the birthdate of the person and that way we've defined our function that gets mapped across all these items and then we just need to make sure our braces here or parentheses are balanced so okay so before I run this let's take a look at this from from like a high level perspective so we're defining this thing here names and ages and that's gonna be a the result of mapping this function here over the list of scientists so this is gonna create a new tuple actually so we can sort treat this like a list it's going to create a new collection or ordered collection that's based on the scientists each scientist it's gonna create a new dictionary here that holds all of these calculated properties so okay I'm just gonna show you what this looks like I'm getting tired here it's pretty late for me I just want to record this video to get it out for you and and this is what I'm doing right nothing happened because we need to print this out and actually this looks pretty ugly so let's pretty print it okay so what you can see here and I'm hoping you're starting to get the idea this took the initial list and it transformed it into a new list that was generated by mapping a function onto each of the original items so now you know this new list here we don't have all of the information we had previously now we have a name and what we have the name and we have to H there's one downside to doing it this way because I you know just defined the fine dictionary here so this again is like immutable this is a mutable data structure because we can reach in and modify this dictionary object so if you wanted to do this better you would probably want to define a new name tuple object but I kind of wanted to do it in one you know one expression here and so this really took the initial list and transformed it into this new list without destroying the original list right and it's kind of beautiful and which again shows you the power of functional programming because you know we have this this well this little map function call here and it it did all of this in one go and it was very declarative we didn't have to you know spell out the loop like oh you know for each X in scientists do this and add it to a list I can all do it in one go which is which is kind of neat now of course you might be thinking you know what this looks kind of not anything like the usual Python code that I see or that I would like to write and I totally get that back honestly I would probably not recommend that you write code that looks like this because it gets confusing it's very hard for people who are new to Python to understand what's going on and it's kind of a different mindset that you're in when you're writing stuff like that so actually what you would probably do to make this more pythonic is you would define a list comprehension so something like this you know again I'm going to define the same fields here X dot name and then I'm gonna add this computed property called age X unborn and then I would just do this for any X in scientists all right there you have it so that's our list comprehension so you would do the same thing typically what you would do it with a list comprehension because that just kind of gives you a more pythonic syntax and and now this isn't equivalent yet so we need to make some some small changes here so first of all I would probably do this if you know if I wanted to make it as close to the map call I would do this with a a generator expression that I just passed his tuple function like this and then I came to we were doing this in one line now and we can pre P print it pretty print it so this is from the P print module if you haven't seen the first installments of the series and we can just print it out and this is what it looks like and get the same result so I would rather write something like this a generator expression and then you know pause post process that or something like this then this map call that we had up here but it's kind of personal preference and both of them it's good to know about these things right it's good to know how these functional programming principles that are available in most other programming languages how they correspond to the Python specifics of having a list comprehension here or a generator expression here in this case right well if you're wondering you know what what this is good for like mapping mapping functions across across a list of things so you know here we could we could do all kinds of things like we could we add all kinds of computed Bertie's right like I could maybe uppercase the the name and you can transform your input data in all kinds of ways and the nice thing is if you if you write your code in this in this fashion where it's functional quote-unquote in the sense that you're taking an input that's an immutable data structure so something that we had up here and you define those transformations in a declarative way like this or with a list comprehension or you know we had further down and and you do this then you have this series of steps that are completely independent from each other you know this what we did down there it didn't modify this list at all it just took this list as an input or this tuple as an input and then generated a new list and so you know we could keep adding more steps to it and they would all be reusable and there would be no side-effects like we're not actually reaching into this thing and destroying this data structure modifying it but instead we're we're creating a new derived list or derived iterable from the input data and that's kind of a cool concept it makes things a lot simpler if you're talking about parallel computation and it's a very very powerful concept just from a mindset perspective not necessarily from the perspective that you should write all of your Python code exactly like this and always use the style of programming that would probably drive some people now here you learned how to use the map function and this is a multi-part series where I talk about functional programming primitives and kind of do a quick intro to functional programming in Python so if you enjoyed this video probably go back and watch the other ones if you haven't seen them yet and I'm going to add a new part to the series soon or we'll talk about the reduced function so the reduced function is another functional programming primitive in Python just like the map and the filter function and you're gonna see how you can use that for for many other interesting things all right so thanks for listening if you want to make sure you don't miss the next episode and then subscribe to this channel and you'll get get to seed right away when it comes out happy Python inge and i'll talk to you in the next video
Info
Channel: Real Python
Views: 38,856
Rating: 4.78125 out of 5
Keywords: dbader, python, functional programming, python map, map function, functional python, list comprehension, generator expression, python fp, python functional, python trick, python filter, python reduce, mutable, immutable, learn python, programming, python 3, pythonic, python tricks, pytricks, python tuple, python namedtuple, python set comprehension, functools, itertools, python lambda, lambda function, lambda, python tutorial, python programming, python list comprehension
Id: powVeMYKCSw
Channel Id: undefined
Length: 13min 23sec (803 seconds)
Published: Wed Sep 13 2017
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.