Locked learning resources

Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Locked learning resources

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Mapping Values and Iterables

Other resources: Python’s map() function tutorial

00:00 In the previous lesson, I demonstrated Python lambdas. In this lesson, I’ll introduce you to the built-in map() function. The map() function takes two arguments, a reference to a callable, which almost always means a function reference, and an iterable.

00:16 What map() does is iterate over the iterable and invoke the callable on each value there. It’s somewhat equivalent to this looping code. I say somewhat because this code creates a list, whereas the map() function returns an iterable mapping object instead. I’ll show you the difference shortly.

00:34 Details notwithstanding, the idea here is to apply a function to each item in an iterable and return an iterable result. Let’s head to the REPL and explore the map() function.

00:46 Let me start by defining something familiar.

00:58 Having a bit of deja vu?

01:06 Well, if you weren’t before, you probably are now. Consider how you might run backwards() on each of the items in animals.

01:21 You could use a for loop like I’ve done here, or you could use map().

01:35 In this second version, map() is invoking the backwards() function on each of the items in the animals list. map() itself returns an iterable, so here the for loop is iterating over the results.

01:51 This is that subtle difference I mentioned earlier. map() doesn’t return a list, but an iterable. The iterable is a map object. Most of the time you won’t even need to think about this as you’re just going to be iterating over it.

02:03 But in the REPL, if you want to see the results, you need to convert it to a list first. Like that. In the previous lesson, I mentioned that anywhere you use a function reference, you can also use a lambda.

02:24 So there in one line is both the definition of the backwards functionality and the execution of it on the animals iterable. Sometimes you need to convert the values inside an iterable to another type to perform an operation on them.

02:37 The map() call can be helpful with this. You’ve probably seen join() before. This call creates a string consisting of each value in animals, separated by, in this case, the plus symbol.

02:52 Some kinds of string operations will automatically convert their arguments to strings, but join() isn’t one of them, and because it won’t convert, this line of code results in an error. You can’t join() an integer.

03:08 To handle this properly, you have to convert the integers to strings first.

03:20 This list comprehension creates a new list converting each number to a string,

03:29 which I can then join successfully. But I can do the same thing with map() and skip the middle step.

03:41 In the list comprehension, I construct a new string by calling the str() constructor. Python doesn’t differentiate between functions and constructors.

03:49 They’re both just callables. So although I’ve been saying functional programming, because that’s what the industry calls it, from Python’s perspective, it really is callable programming.

03:59 Passing str as my callable means it gets invoked for each item in the list, which for str means converting the integer to a string.

04:08 The result is an iterable of strings which join() can then use to do its work. You can also give map() two iterables. When you do, it invokes the callable with two arguments, creating pairs of each of the corresponding index items in the set of iterables.

04:34 Let’s break this down a part at a time. I’m passing a lambda to map(), which takes two arguments and returns their sum. Since I’m using multiple iterables in map(), each invocation of the lambda will use the corresponding index value from the set of iterables.

04:50 So the first invocation sums 1 and 10 from the first position of each of the iterables. The second will sum 2 and 20, etc, giving the end result of 11, 22, and 33.

05:04 All of the operations that you do in Python, like adding, multiplying, and even comparisons, actually invoke corresponding dunder methods in the objects.

05:12 If that’s not funky enough, the operations themselves are abstracted as functions that you can use. They’re in the operator module. I can use the add() function to replace my lambda above.

05:33 Same idea, less typing. You can also use more than two iterables with map(). You just need to make sure that the callable has the corresponding number of arguments.

05:54 This is similar to above, but using three iterables instead. In this case, you can’t use the add() function as it only accepts two arguments, so you have to use the lambda.

06:04 Although you could have been clever and used the sum() function instead. Part two of the functional programming triumph is filter().

06:13 That’s next.

Become a Member to join the conversation.