How to utilize the map() Function
The built-in function map()
takes a function as a first argument and applies it to each of the elements of its second argument, an iterable. Examples of iterables are strings, lists, and tuples.
For more information on iterables and iterators, check out Iterables and Iterators. map()
returns an iterator corresponding to the transformed collection.
00:00
Next up, map()
. Just like filter()
, map()
takes two arguments: a function
and an iterable
.
00:08
But it works differently. It returns a new iterator by applying the function to every item of the iterable
it’s been passed, yielding those results.
00:19
It allows quick application of a function to every element of an iterable. Here’s an example where we’re passing it the numbers 1
, 2
, 3
, and 4
and the lambda expression squares the input number.
00:34
This gives us a new list with the squares of the original numbers—[1, 4, 9, 16]
. Now let’s see map()
in action in a little more detail.
00:49
As you’ve just heard, map()
applies a function to an iterable to create a new iterable. In this case, we’ll be supplying it with a list of numbers and using a lambda function to square those numbers to create the new list. Firstly, we’ll need a list of numbers, and we’ll print that list out.
01:13
Next, create the new list of numbers with a lambda expression which will square any value that’s passed to it. And finally, print out the new list. Let’s see that running. And here, the first list is the original list of numbers from 1
to 10
, and the second list is the square of those values.
01:47
The lambda expression being used in a map()
function doesn’t have to generate the same kind of data as has been passed into it. So here we’re going to see one where we get either a True
or False
result.
02:00 Again, we’ll start with a list of 10 numbers.
02:08
Next, we’ll use map()
to create a new list and have a lambda expression which generates True
or False
depending on the condition.
02:22
Here you can see this expression will generate either True
or False
depending on whether x % 2
is equal to 0
or not.
02:32
Now we’ll print the new list out. Let’s see that running. And as expected, we have our list of numbers from 1
to 10
, but we also have our output from our map()
, which has generated either False
or True
depending on whether the numbers are odd or even.
03:03 Now you’re going to see an example where the lambda expression returns a tuple, modifying the contents of the tuple it’s been passed.
03:29 Here’s a list of tuples containing team names, and the number of times they’ve taken part in the World Cup finals.
03:41
We’re going to print that out, and now we’re going to use map()
to apply a lambda expression across the entire list of tuples, incrementing the number of times each team has taken part.
03:52
This is of course assuming that all these teams have made it into the next finals. Next, we create a new list using map()
on a lambda expression which takes a team
tuple, and outputs a new tuple where the first element is the unchanged team name, and the second element is the incremented number of times the team has taken part.
04:26
Finally, we pass in the wc_teams
list of tuples to map.
04:37
The last step is to print out this new
list of tuples. Let’s see that code running.
04:54
Here you can see the original list of tuples with the team name and the number of times each team’s taken part. And secondly, you can see the new
list of tuples with the unchanged team names and the incremented number of times the team has taken part.
Become a Member to join the conversation.