- [Instructor] In this video, we'd like you
to experiment with the capabilities we just demonstrated
with the filter and map functions in particular.
So, for the purpose of the first exercise here,
we'd like you to create a list called numbers
that contains the values from one through 15,
and then we've got three tasks for you
to perform using that numbers list.
In the first one, we'd like you to use a filter
with a lambda to select only the even elements
of numbers and to create a new list containing that result.
In the second one, we'd like you to use the map function
with the lambda to square all the values
in numbers' elements.
And then in the third, we'd like you to use filtering
for numbers to get just the even elements,
then map those even numbers to their squares.
Now, we have a second exercise as well
that we'd like you to try.
We want you to map a list of Fahrenheit temperatures
to a list a list of tuples, not just individual items,
but tuple objects, that contain both those Fahrenheit values
and their corresponding Celsius equivalents.
So this is your equation for calculating Celsius
from Fahrenheit temperatures.
So we would like you to create a lambda
that does that, and use it with mapping
to create a new list containing those tuples described here.
So go ahead and pause the video, and then come back
to see the answers.
Okay, let's go back to this first exercise.
As you can see, we've already left
for you the statements here for creating a list
of values from one through 15,
and evaluating that to see what it contains,
just to make sure it worked correctly.
So there's our numbers from one through 15.
And again, the first thing we wanna do here
is filter out only the even elements.
So we wanna create a new list
that contains just those even elements.
And again, we could do this
with list comprehensions, but here, we wanted you
to specifically do it using the filter function.
So if the value of a given element is divisible by two,
the result will be zero, so we've created a lambda
for that purpose.
We're going to apply that lambda
to each individual element in the numbers sequence,
and the filter function will then give us back an Iterable
of just the even numbers, which we will then pass to list,
and it will iterate through the results,
placing each item into the resulting list.
So there is our set, or our list, rather,
of the even-numbered values
from the original sequence called numbers.
Next, we wanted to do a mapping operation
that would square each of those values.
So, same basic concept here, but now we're calling map
with a lambda, that instead of returning true or false,
returns a new value for each individual item passed to it.
So for whatever x is, we would like to square x,
and we will apply that to each element
of the numbers sequence.
And the list function will then create the resulting list,
containing all those values.
Now, for the last operation, we wanted
to combine those items so that we filter
to get just the even numbers, then map those elements.
So here, we have a list call that's going
to receive the result of a mapping operation.
The mapping operation is going to apply the map
to the results of a filter operation.
So let's start with that.
We're going to filter to get just the even elements
from numbers.
Those elements will then be mapped to the squares
of those elements, and those elements will then be used
to create the list.
So you can see just the squares of the even numbers
that we had produced up above.
Now separately, as we discussed previously,
mapping does not have to create objects of the same type.
In this case, we're going to take numeric values
and turn them into tuples containing pairs
of numeric values.
So we're going to go from one numeric value to a tuple.
That's a change in type.
And then within the tuple, we will have numeric values.
So first, we're going to create this Fahrenheit array,
just so we have the values to process.
And this is the expression that we're going to use,
or the call that we're going to use, to produce the results.
So starting with the map operation here,
what we're saying is that for the elements of Fahrenheit,
we would like to apply this lambda expression,
which is going to take each Fahrenheit temperature
and then produce a tuple.
Notice the parentheses wrapped around this here.
Going to produce a tuple containing the value of x,
which is the Fahrenheit temperature,
separated from the next item,
which will be the corresponding Celsius value produced
with the calculation you see up above here.
So Fahrenheit minus 32 times 5/9ths
will give us the corresponding Celsius temperature.
And then the list function will take those mapped values
and create a list of tuples for us.
So we see here, 41 maps over to five,
32 maps to zero in Celsius, and 212 maps
to 100 in Celsius degrees.