How to Use the map() Function
In this lesson, you’ll learn how to use the
map() function in Python in order to apply a function to all of the elements of an iterable and output an iterator of items that are the result of that function being called on the items in the first iterator.
You’ll walk through a practical example of how to use the
map() function with the example data set that you’ve been working with in this course so far. You’ll know how to get a new iterable containing all of the information that you need from the old iterable, but you won’t modify the old iterable in the process!
What we’re going to do now is we’re going to use the
map() function to do some interesting stuff with it. Typically, you want to use the
map() function if you want to take something like this, like this list here, and you want to transform it into a different list. Basically, all it does—it takes a list of stuff, applies a function to each item, and it assembles a new list based on that. 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, we’re going to ignore most of these fields here and we’re going to focus on the
name and then some calculated property that’s based on the
born field and the birthdate.
Here again, we’re going to use the
tuple() function, first, to convert the output of the
map() function into a tuple. I’m going to show you what it looks like without the
tuple() call, but just so you get a proper output that’s actually more interesting.
we’re going to define this function that we want to map. Here, I’ll do it with a lambda, which is just a one-line function in Python that has a single—it can take arguments, and then it can… It has a single expression here and it will just automatically return the result of that expression. All right, so what are we going to do here? In this case, I’m just going to define a dictionary object, for lack of a better idea, and we’re going to plug in the
.name and we’re also going to create this new field here called
Okay. Before I run this, let’s take a look at this from, like, a high-level perspective. We’re defining this thing here,
names_and_ages, and that’s going to be the result of mapping this function, here,
over the list of scientists. This is going to create a new tuple, actually, so, we can sort of treat this like a list. It’s going to create a new collection, or ordered collection that’s based on the scientists. For each
Scientist, it’s going to create a new dictionary here that holds all of these calculated properties. So, okay. I’m just going to show you what this looks like.
02:53 All right. Nothing happened, because we need to print this out and, actually, this looks pretty ugly, so let’s pretty-print it. Okay. 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
So now, in this new list here, we don’t have all of the information we had previously. Now we have a name—well, we have the
'name', and we have the
'age'. There’s one downside to doing it this way because I defined a dictionary here, so this—again—is, like, a mutable—this is a mutable data structure because we can reach in and modify these dictionary objects. So, if you want to do this better, you would probably want to define a new
namedtuple object, but I kind of wanted to do it in one expression here.
So, this really took the initial list and transformed it into this new list without destroying the original list, right? Which is kind of beautiful and which, again, shows you the power of functional programming, because we have this little
map() function call here, and it did all of this in one go and it was very declarative. We didn’t have to spell out the loop like, “Oh,
for each x in scientists do this and add it to a list.” I can all do it in one go, which is kind of neat.
Become a Member to join the conversation.