Search⌘ K

Map

Explore how to use Python's map function to apply transformations to iterables efficiently. Understand lazy evaluation with map, handling single and multiple argument functions, and how to work with iterators returned by map for processing sequences.

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.

Python 3.8
def square(x):
return x*x
a = [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 ...