Map
Learn how to use the function, "map", to transform iterables by applying a function to them.
We'll cover the following...
map
function
The map
function applies a supplied function to a set of arguments. It returns an iterator to access the results.
map
with one parameter
In this example, we will use map
with a user-defined function, square
, that takes one parameter.
Press + to interact
def square(x):return x*xa = [2, 5, 6]m = map(square, a)print(list(m))
The map function applies square
to each element in a
, returning the squared values via an iterator. On line 7, we convert m
to a list and print it.
Lazy evaluation
This is perhaps a good time to revisit the idea of lazy evaluation. All the functions described so far use lazy evaluation. We will illustrate this by ...