Here are resources and additional documentation about lambdas:
Getting Started With Python's map() Function
And as we saw in the introduction, the idea of using the
map() function is that you want to evaluate
function at every element of the
iterable. Now, the first positional argument
function can be any Python callable, such as built-in and user-defined functions, classes, methods, and
map() returns an iterator that yields items of the original
iterable transformed by
That means that the iterator returned by
map() will first take the first element of each iterable and use those as arguments into the
function(), and so then that would yield the first item returned by the map iterator.
Now, one of the nice features with the
map() function is that the length, or the number of values yielded by the iterables that are passed into
map()—that length doesn’t have to be the same for all.
And so what’s going to happen is that the number of values that are yielded by the iterator that’s returned by
map() is going to equal the smallest number of values that are yielded among the N iterables that are passed to
Now, I think for you to fully understand this statement, you just have to start using
map(). I’m not completely advocating that you abandon
for loops and list comprehensions, but you’ll see that every now and then your main problem is just to simply map a function to an iterable, and just simply writing that using the
map() function feels a lot more natural than just having to write out the details of an iteration. Now, a technical reason why you may want to use the
map() function is that it’s written in C and is therefore highly optimized.
The internal loop used in the
map() function can be more efficient than just the regular Python
for loop. Another technical reason why you may want to use the
map() function is that it returns an iterator, and so items are processed on demand, resulting in more efficient memory usage.
Another reason why you may want to use the
map() function is that when you have multiple iterables, it may be clearer to use the
map() function than using an equivalent list comprehension and the
We’ll make this comparison in a later lesson so that you can see for yourself whether in some cases it is maybe a little bit more readable to use the
map() function over, say, a list comprehension with the
As we saw, the
map() function takes in a positional argument
func, which is going to be a function, and then a certain number of iterables, and the number of iterables is going to equal the number of positional arguments taken by the function
Let’s start off with a very simple example, just so that you can see how the
map() function works. Let’s create a list of numbers and we’ll call the list
numbers, and the list is simply just going to contain, say, the numbers from
And what you want to do is simply square each of the numbers in the
numbers list, and then either save the information in an iterator or maybe just a list. So go ahead and define a function, we’ll call it
square(). And of course maybe in your application, you have a much more complicated function, but this will do for this simple example.
The function is just going to return
x squared, or
x to the power of
2. Now let’s call
map() with the
square function as the first positional argument and the
numbers list as the only iterator, because
square() only takes in one argument.
map, and as we saw in the
map() returns a
map object. Now, we can do several things with this
sq_nums. If we need it for, say, a
for loop, then we can just use it as if it’s any other iterator. For example, we could just print the values obtained or yielded by this iterator.
for num in sq_nums: print the value of
num. And in this case, nothing happens or nothing’s printed. If you’re not familiar with iterators, an iterator—which is what
sq_nums is—returns a value on demand and once all of the values that are yielded by the iterator are exhausted, then nothing else is returned and internally there is a
StopIteration error that happens in the
for loop, and it’s taken care of by Python and so we don’t see any type of error message—we just simply see nothing printed to the screen.
And then now,
sq_nums is just a good old-fashioned list. And if we run that same
for loop again, of course, we get the same print statements, and of course
sq_nums is still a good old list. Again, that’s just something to keep in mind: The
map() function returns a iterator object.
And if all you need that iterator object is, say, for one
for loop and that’s all you need it for, then that’s probably what you’ll want to do, especially if it’s a iterator that yields a lot of values.
You don’t want to store a large list in memory if all you’re going to need it for is, say, one iteration or one
for loop, for example. You’re better off creating an iterator, and the
map() function does that for you.
The first positional argument to the
map() function—it can be any callable. So it could, for example, be a
lambda function. Now, the
square() function that you defined—it’s a fairly small function, it only takes one line of code, so that’s a pretty good candidate for a
lambda function. So we could, instead, if we were to go ahead and use a
lambda function, we would simply write
map() and then
lambda x and then just simply return
x squared. And we’ll use the same iterator of course.
If you’re not familiar with
lambda functions, there’ll be some more information in the notes that accompany this lesson. Let’s run that again. This is essentially the same thing, except this time we are passing in a
lambda function, which is a anonymous function. It has no name, it’s just simply defined for the sole purpose of being passed in as a argument to this
map() call. So, if we run that same
for loop again, we’re going to get the exact same result. There we’re just simply printing the numbers in the
And then we have a list of y-coordinates, and this is going to be
-0.5, and so on. Now, I don’t know if you remember this from, say, your math classes in grade school, but if you want to know the length or the distance from a point in 2D space to the origin, the formula is square root of the point x—or, the x-coordinate squared plus y squared.
And then, you know what? Why don’t we just do a
lambda function inside the
map() call? What we want to do is we want to map the function that takes two positional arguments—these are going to be x- and y-coordinates—and it’s just going to return the square root of the sum of
x squared plus
Let’s save that in, say, a variable called
distances, so we’ll add the
s there. And that’s it! What’s going to happen here is the iterator obtained by the map object will one at a time first take an x-coordinate and a y-coordinate and evaluate this function that takes two positional arguments, and will return this value for the first
x and the first
So that’ll be the first value yielded by the map iterator—the iterator returned by
map(). And then the next value is going to be obtained by taking the second x-coordinate and the second y-coordinate, evaluating this function—this
lambda function—and then yielding that value.
Now let’s compare this with the values that are going to be yielded by the
distances iterator. So
for d in distances: go ahead and just print these values that are yielded by the
distances iterator. And as we saw, the first one—
3.98, the second is
2.25, and the other two are given below. Now, let me show you just one last thing. If, for example, the x-coordinates list—let’s say actually it only had three x-coordinates and the y-coordinates had the same number as before, so in this case four. Then if we run the exact same code, so
distances, and we go ahead and print all of the values that are yielded by the
distances iterator, we’re only going to get three, and that’s because the iterator returned by
map() will yield as many values as the shortest iterable that’s passed into it.
So, this is a nice feature of the
map() function. It’s not going to give you an error, it’s not going to raise an error. It’s just simply going to iterate until it can’t and that’s going to be determined by which iterable passed into the function has the shortest length. All right!
This ends the basics on the
map() function. In the next couple of lessons, we’re going to be doing some more examples of using the
map() function, and sort of part of that is just to get you thinking about when you would want to use the
map() function and if it’s something that you want to do. And, you know, the more you try it, you’ll see that maybe the
map() function is something that you may want to try every now and then instead of, say, using a
for loop or even a list comprehension. We’ll talk about that later, also, in a future lesson.
Become a Member to join the conversation.