Python's map() Function: Transforming Iterables (Overview)
Python’s map()
is a built-in function that allows you to process and transform all the items in an iterable without using an explicit for
loop, a technique commonly known as mapping. map()
is useful when you need to apply a transformation function to each item in an iterable and transform them into a new iterable. map()
is one of the tools that support a functional programming style in Python.
In this course, you’ll learn:
- How Python’s
map()
works - How to transform different types of Python iterables using
map()
- How to combine
map()
with other functional tools to perform more complex transformations - What tools you can use to replace
map()
and make your code more Pythonic
00:00
Hey there! I’m Cesar. Welcome to this Real Python video course on the map()
function and some general ideas in functional programming.
00:10
Let’s do a quick overview of what you’ll learn in the course. We’ll start off by getting a basic understanding of the map()
function through some examples to help you identify when it is that you basically want to map a function at every element of an iterable.
00:27
You’ll get a lot more practice as we go through some examples where we’re manipulating iterables of strings with the map()
function and you’ll even get a chance to write your own version of the Caesar cipher.
00:39
We’ll then take a look at combining the map()
function with two other important functions in functional programming, and these are the filter()
function and the reduce()
function.
00:50
We’ll then take a look at a very useful function that’s in the itertools
module called the starmap()
function, which is very similar to the map()
function but is tailored to when the iterables that you want to map to are all contained in one iterable. We’ll then end the course by going over some Pythonic alternatives to the map()
function, such as list comprehensions and generator expressions.
01:17
Even with that being the case—that there are some Pythonic alternatives to the map()
function—it’s good to have a good general understanding of the map()
function and the filter()
and reduce()
functions. Now, before we get into the details about how the map()
function is implemented in Python, let’s go over these basic three ideas that are mapping, filtering, and reducing data.
01:41 Now, all of these three functions—mapping, filtering, and reducing—are three important ideas in functional programming. In functional programming, programs are constructed by applying and composing functions using a declarative programming paradigm.
01:58 One of the basic ideas in functional programming is that you write your programs focused more on sort of what it is that you want to do, and not so much about the nitty-gritty details on how to do it, and you’ll see what I mean by this through some of the examples that we do.
02:15 In functional programming, functions are first-class citizens, and what this usually means is that they can be assigned to variables and they can be arguments to functions and they can also be returned by functions.
02:29 Now, there are three common operations in functional programming, and these are mapping, filtering, and reducing data. Let’s do a quick overview of these basic operations.
02:42 So first of all, mapping consists of applying a transformation function to an iterable to produce a new iterable. The items in the new iterable are produced by calling the transformation function on each item in the original iterable.
02:59
One of the common ways that you’re going to be applying the mapping idea is on a list. You have a list of elements and you have some function f()
, and what you want to do is simply create a new list obtained by mapping this function f()
at each element of this list to produce this transformed list. Now, thanks to the ubiquitous use of iterables and iterators in general in Python, you can apply this mapping idea not just to lists but to any type of object that can return on demand, one at a time, its elements.
03:37
The other key operation is the filter one. A filter applies a Boolean-valued function to an iterable to generate a new, possibly smaller, iterable. The items in the original iterable that return a True
value are the ones that are kept to produce the new, possibly smaller, iterable.
03:58
So again, if we have a list of elements and we have some function f()
that returns essentially either True
or False
, then the elements where f()
evaluated at each element returns True
are the ones that are kept in the new smaller iterable.
04:16
So in this hypothetical scenario, the values in this list, a_0
, a_1
, a_3
, and a_5
—all of those return a True
value after being applied on by the function f()
.
04:30 And so those are the new elements that are kept in this filtered iterable.
04:37 The last key operation that we’ll take a look at in functional programming is a reducer, or sometimes called a fold or an accumulator.
04:47 Now, a reducer applies a binary operator—so, an operator that takes two arguments—and this operator is applied recursively to the elements of the original iterable to produce a single cumulative value.
05:02
Now, a lot of operations that you’ve already probably used before, a good example of which is the sum()
function in Python, is an example of a reducer or what can be considered a reducer.
05:14
The basic idea is that the reduce()
operation will take this input function f
and apply it to the first two elements of this list, and the return value of that operation will be the first positional argument to a call of f()
, where the second positional argument will be this third element of the list. And recursively, that’s how the accumulated value is obtained, producing the value y
. We’ll see some examples of this, so if you haven’t seen this before—at least not talked about it in this way—you’ll get a chance to unravel this.
05:51
All right! Well, that was just a quick overview of some of the things that you’ll learn in the course. In addition to learning about map()
and reduce()
and filter()
, you’ll get a lot more practice with working with iterators in Python.
06:05
And iterators in Python are everywhere, so if you’re kind of new to the idea of iterators, this course will also help to remove some of the mystery behind what iterators are and how to work with them. All right! In the next first lesson, we’ll jump right into it and introduce to you the map()
function.
Become a Member to join the conversation.