Combining map() With filter()
Here are resources and additional documentation about tuples, packing, and unpacking:
00:00
In this lesson and the next, we’re going to combine the map()
function with two other functions that arise frequently in functional programming, and these are filter()
and reduce()
.
00:11
Let’s start off with filter()
. The filter()
function takes two positional arguments. The first one is a function, and the function is going to return either True
or False
or some object that Python can convert to a Boolean.
00:26 And the second positional argument is an iterable.
00:30
The idea of the filter()
function is that it’s going to return an iterator yielding the values in the iterable for which function()
returns True
.
00:40 Let’s see a quick example of this. All right, so in this example, you’ve got a list of strings and you want to convert the strings in this list that are considered integers because you then want to go ahead and do some sort of computations with these values.
00:57
Now, this third string in the list, although it contains digits and dashes, if you try to call the int()
function on this string, you’re going to get a ValueError
.
01:08
And so what you want to do is you want to filter out all of these types of strings. And so with the filter()
function what we’ll do is we’re going to pass in data
as the iterable and then use the .isnumeric()
function from the str
class to only keep those strings that are considered numeric.
01:28
The iterator returned by filter, we’re going to save it in the numeric_data
variable, and then we’re going to take that numeric_data
iterator and use it as our iterator into a map()
call where the function is going to be the built-in int()
function in Python.
01:45
This will take the numeric_data
iterator and convert all of those strings into integers. And then if we call the list()
function just to see what the final list is, we get the list containing the integers 387
, 283
, and 361
.
02:01
All right, so that’s just a quick illustration of how the filter()
function works. Let’s take a look at another example.
02:09
Let’s take a look at an example that combines the filter()
and the map()
function. In the example, we’re going to be working with colors, and in particular we’re going to be using the RGB color model. In the RGB color model, you mix a little bit of red, a little bit of green, and a little bit of blue to specify the color that you’re after. These are called the RGB channels, and a common way to specify these values is to give an integer between 0
and 255
. So for example, the tuple (255, 0, 0)
specifies the color red.
02:51
Whereas, for example, the tuple (0, 0, 255)
specifies the color blue. Now, to get orange, for example, you have to mix all the red and about halfway of the green. Now, as a side note, because each of the channels can take on a value between 0
and 255
, you can produce with the RGB model, theoretically, 256 to the power of 3 different colors, which is around 16.8 million different colors.
03:30 All right, so what’s the problem here? Well, you have a list of colors. I’ve written these out using the RGB model. These are tuples containing three elements.
03:40 And you can get this list by copy pasting from the code that’s provided in the course or you can just simply write these out. There’s not that many.
03:49 So, the problem that you want to solve is you want to convert each of these RGB tuples into tuples where instead of integers you have floats that specify the percentage of the channel.
04:02
So, for example, for the first color of (200, 120, 40)
, your application accepts only percentages for the channels, and so what you need to do is convert each of these channels to a float between 0.0
and 1.0
, and so the color would be specified with this tuple of floats.
04:25 You want to do this for each of the RGB colors in your list. But you don’t want to do this for all of them. You’re only concerned about the colors that are considered green or are greenish.
04:37 Now, I’m not an expert in color theory, but if I was asked to just sort of give an elementary way to determine when a color is considered greenish, what I’d probably do is say, “Well, if the green channel is higher than the blue and the red channel, then I’m going to consider that RGB color greenish.” So, that’s what we want to do.
04:56 We want to filter out and keep only the colors that are greenish, and for those greenish colors, we want to do this normalization.
05:07
All right, let’s write out our filter function. So, the filter function—let’s call it, say, isgreenish()
. Remember that the function that you’re going to pass into the filter function should return a Boolean.
05:20
Our isgreenish()
function is going to take an rgb
tuple. We want to unpack those channels, and so we’ll use Python’s unpacking notation.
05:31 If you’re unfamiliar with how Python unpacking works, I’ll leave a link so you can look it up. But basically what we’re doing is this tuple that’s going to be the input will be a tuple containing three elements.
05:44
The first element will be saved in the variable r
, the second in the variable g
, and the third in the variable b
, representing our red, green, and blue channels.
05:55
And then what we simply want to do is return whether g
is greater than r
and g
is greater than b
. This is going to be our way to decide whether an RGB color is greenish. All right, so that’s our filter function.
06:13
Now let’s define our normalization function. We’re going to call it normalize()
and this is also going to take an rgb
tuple. What we want to do is take each channel and divide it by 255
.
06:28
So, why not use the map()
function to do that? We’ll use a lambda
function. It’s going to take one input, it’s going to represent the channel, and we just simply want to return the channel value divided by 255
, and we want to do this for each of the elements in the rgb
tuple.
06:48 And so that we don’t get a lot of decimal places, why don’t we round this float so that only two digits after the decimal are kept? Now, as you know, this is going to return an iterator, and so what we’ll do at the very end is create a tuple out of it so that we get a tuple of RGB channels that are in percentage, and that’s going to be our final return value.
07:13
So, this is our normalize()
function. All right, so let’s clear that up. Let’s go back here. We’ve got our colors
. What we wanted to do is we wanted to map our normalization function to the colors that are greenish, and so let’s filter out and keep only the colors that are greenish on the list of our colors.
07:36
And maybe we’ll save this iterator in a variable called green_colors
. All right, so go ahead and run that and let’s take a look at these colors as a list.
07:49 And there we go. We have our filtered greenish colors normalized so that instead of integers, we’ve got floats up to two decimal places for the channels.
08:02
All right. I hope you enjoyed the lesson on how to combine the map()
function with the filter()
function. In the next lesson, we’ll take a look at how we can do something similar by combining the map()
function with the reduce()
function.
Become a Member to join the conversation.