Why Use the map() Function?
Now that you know how to use the
map() function, you’ll review why you might want to use it. You can use this function to write your code in a functional way to input an immutable data structure and define the transformations you want to make, in a declarative way.
When you take this approach, you have a series of steps that are independent from each other and don’t modify your data set. This allows you to keep adding more steps to your program, avoid side effects, and ensure that your data set is still usable.
00:00 Well, if you’re wondering what this is good for—like, mapping functions across a list of things—so here, we could do all kinds of things. Like, we could add all kinds of computed properties, right?
00:18 all kinds of ways. And the nice thing is if you write your code in this fashion where it’s “functional”, 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
00:36 declarative way like this, or with a list comprehension, or, you know, what we had further down. And you do this, then you have this series of steps that are completely independent from each other. You know, what we did down there—it didn’t modify this list at all.
00:54 It just took this list as an input—or, this tuple as an input, and then generated a new list. We could keep adding more steps to it and they would all be reusable and there would be no side effects.
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 this style of programming. That would probably drive some people nuts. Here, you learned how to use the
map() function. All right!
Become a Member to join the conversation.