Detailed Notes
We look at higher order functions in Python for this episode focused on getting ready for certification.
Useful Links: https://www.codespeedy.com/higher-order-functions-in-python-map-filter-sorted-reduce/
Learn more about the overall certification and syllabus here: https://pythoninstitute.org/certification/pcap-certification-associate/pcap-exam-syllabus/
Github repository for code examples: https://github.com/robbroadhead/PythonCertificationSeries
Transcript Text
[Music] well hello and welcome back we are continuing looking at our python certification uh series of tutorials this episode we're going to look at what are called essentially higher order functions now we're going to look at map filter reduce reversed and sorted these are good followers uh follow-ons to the uh the whole lambda concept because it gets into the idea of um sort of recursion and some things like that that just i think once you unders you've got a rough feel for lambdas then these kinds of things make sense or maybe vice versa these may help you understand a lambda a little bit better the first one i'm going to look at is map so let me just do this and the way map works is that you give it two parameters let's see here so what we're going to do is we're going to use this add one that we used before we had this add one method function actually and all it does is it just returns uh whatever value you send it and it adds one to it and then returns it so what we're doing here is we're going to call add1 and we're going to send it an array and that's what map does is it says take this function and then walk through the array and then just call the function with each of those values so what we're going to see here and let me go ahead and do that let's clear this up and so the first thing we do let's flip back over here is we store the result in the sinkhole's results now if you look at results itself if you try to present it print it it's going to return a map object so you actually get an object back you get this essentially this collection of results so we can do is we can just call list and i care if we've seen this before but basically all this does it says take this collection and turn it into a list and we can print that out and we can see that when we send 2 4 6 8 10 it gives us back 3 5 7 9 and 11. so we've got an easy way to apply a function across a series of stuff now similarly let's create one that's going to be let's do is ant well let's call it is gnome oh let's just call actually let's get this one really good let's call it is blank and it's going to take x which is going to be a string and it's going to return a boolean and it's going to say uh it's just going to be let's say return let's say uh let's do length of that less than one so well let's do it this way let's clean it up a little bit and then return results just in case i have an issue with that and it probably doesn't like this let me look at this is that maybe what it wants because it's probably complaining that's not a boolean oh sorry [Music] there so now if i just do let's first do this example of this is going to be filter and create a values 2 here and we're just going to do this [Music] and we're going to add a couple of empty strings and we're going to essentially the same thing so for this time we're going to do filter use values 2 and we're going to call it it's blank and let's call his blank first so let's just do print is blank and we're going to look at these we're going to do uh this guy and then we're going to do them again [Music] let's see let's see for this one's going to be an example of not blank and we're going to do an example of his blank so let's play around with this one a little bit so let's go take a look at that and let's see what we come up with so here uh let's see so the example filter doesn't do anything so example of not blank so we're gonna we have something here oh i'm sorry that's not what we want so let's just do uh equals uh not oops x did i do that right before oh let's do this that may give me what i want except for let's try it okay so let's go play with this for a second there we go okay i'll just use the right variable so uh not blank gives us a false if it is blank it gives us a true i guess since that's a print i don't need that so now what we can see here is this is filtering based on true so you can see here we did if it's blank then it's going to keep it so we would say so if we switch it and go to not blank and then uh just reverse that well let's just do this uh if it is greater than zero then it must be not blank and we're gonna see that oops ah where did i do that oh sorry i have to change the name here and once i change it make sure i save it let's clear it just because and go here and so now notice i still get i get a filter object instead of a map object but i can still do a list on this and now i'm just filtering out everything that is not blank so that gives me a nice little you know way to go through a list of some sort and filter it out no pun intended based on that name so now similarly now let's do sorted next and let's just do uh we'll do sorted based on values let's see so let's go oh okay i just had something weird going there we go [Music] okay well my keyboard has got something stuck there okay so example of sorted and i'm going to do our results equals let's do this here wow i've got some weird stuff going on here [Music] let's try this again all right well let's do this [Music] wow that is some craziness let's do this okay so now let's go back to that okay so example sorted uh i just want to do it like i did with the map so let's do those three except for sorted is not going to take anything but the array results results okay and so now if we go back here clear it and run it we'll see oh let me that's let's do reversed well let's do this let's move these around a little bit so let's 2 2 6 four ten eight and now we can see that sorted is going to give us that sorted value and notice here that the results is actually a list so sorta is going to return to list if i want to do reversed i can do [Music] i'm just gonna do it that way and reversed how however gives us a reversed object [Music] and so we have to go look at it and see that in this case it's not this is important there's two things here sort is probably the easiest or sorry sorted is the easiest to deal with as far as it returns you a list reversed which you would think does sort but it's reversed you can actually do let's do sorted i'm going to reverse it and i want it to be uh reverse [Applause] [Music] equals true so i think that's a capital t that so now we'll see here it is reversed so i've got sort and i've got it sort of reversed ordered what reverse does is not related to sorting the values but just the keys themselves so we can see our original was uh here 2 4 oh i'm sorry uh where'd it go oh i don't have it so our original up here was 2 6 4 10 8. if we look it's 2 6 4 10 8 reversed so it's reversed the order of the items within it not the sort order if you want to reverse a sort order then you have to do reverse equals true and that leaves us one left which is reduced and i can actually do that sort of similar to what i had before where i go here we go add one okay so now what reduce does is it's going to take [Music] it's going to apply the function oh sorry reduce is actually in funk tools so let's see if that i may have to import that so let's see just a second so although you will see it probably on the certification exam so what i do here is i'm going to give it the values but what it's going to do it's going to add the function but then it's going to keep applying uh additional stuff to it so when i do add one then it's just going to throw in the next one and it's eventually going to get it down it's going to take the results and you typically well i'm sorry i can't do that here as i'm thinking about it i actually need i'm just going to call it let's call it add vowel and this one's going to take an x and a y uh because uh return x plus y what it does with reduce uh let's see that work blah blah blah let's get that there return x plus y that looks good and then uh reduce is going to reduce add when i call it add one no add val and what it's going to do is it's going to go through and take the first two items because of those parameters in this case it'll take uh so 2 and 6. so it's going to add those together and give you eight but then it's going to take that result and then tack on the next one so it's going to call advil again with eight and let me do this called with and i'm just going to do a string x and let's do it this way and string y and we will see that it's going to add those things up and it's going to keep tacking values on and using the result as the first value as the first parameter so if we go back here let's clear it out because it's going to give us a bunch of stuff and we'll see here so first it comes in with two and six it adds those together so it does two and six adds those to get four it does eight and four adds it together get 12. then it calls 12 with the 10. close that together get 22 cause 22 with 8 and boom we end up with 30 which is uh here and we're going to find in this case because it returns that final value which is an integer we get an error because we can't do a list on an integer it's not iterable as they say you can't go across it so that gives us a couple of and that's a few very valuable ways to deal with sets of some sort to deal with arrays in particular values we can use our filters to reduce stuff out to to filter stuff out we can use map to just call you know use that function against every item within the collection within the array we can do reduce to continually call that thing uh that to continually do that function and then feed its result back in and start working our way down the list of parameters we can do reversed remember reverse reverses the items in the array which is not the same as reversing the sort uh the sort order we can use sorted to sort them and we can do sorted with where we have here and add reverse equals true to send those in reverse value that's uh that's pretty good set of stuff today so we'll let it get to it as always you can find stuff in the github github repository and uh stick around next time we will just continue working our way through these we're actually getting pretty close so we're getting close to the point where if you've been following through with this you probably can be in pretty good shape to go take a stab at a certification that being said let's get to it so go out there and have yourself a great day a great week and we will talk to you next time you
Transcript Segments
[Music]
well hello
and welcome back we are continuing
looking at our python certification
uh series of tutorials this episode
we're going to look at
what are called essentially higher order
functions now we're going to look at map
filter reduce reversed and sorted
these are good followers uh follow-ons
to the uh
the whole lambda concept because it gets
into the idea
of um sort of recursion and some things
like that that just
i think once you unders you've got a
rough feel for lambdas then these kinds
of things make sense or maybe vice versa
these may help you understand
a lambda a little bit better the first
one i'm going to look at
is map so let me just do this
and the way map works
is that you give it two parameters
let's see here so what we're going to do
is we're going to use this add one
that we used before we had this add one
method
function actually and all it does is it
just returns
uh whatever value you send it and it
adds one to it and then returns it
so what we're doing here is we're going
to call add1
and we're going to send it an array and
that's what map does is it says
take this function and then walk through
the array and then just call the
function with each of those values
so what we're going to see here and let
me go ahead
and do that let's clear this
up
and so the first thing we do let's flip
back over here
is we store the result in the sinkhole's
results
now if you look at results itself if you
try to present it
print it it's going to return a map
object so you actually get an
object back you get this essentially
this collection of results
so we can do is we can just call list
and i care if we've seen this before but
basically all this does it says
take this collection and turn it into a
list
and we can print that out and we can see
that when we send 2 4 6 8 10
it gives us back 3 5 7 9 and 11.
so we've got an easy way to apply a
function across a series of stuff
now similarly
let's create one that's going to be
let's do is ant
well let's call it is gnome
oh let's just call actually let's get
this one really good let's call it is
blank
and it's going to take x which is going
to be a string
and it's going to return a
boolean and it's going to say
uh it's just going to be let's say
return
let's say uh
let's do
length of that
less than one so
well let's do it this way let's clean it
up a little bit
and then return results just in case
i have an issue with that
and it probably doesn't like this let me
look at
this is that maybe what it wants
because it's probably complaining that's
not a boolean
oh sorry
[Music]
there so now if i just do
let's first do this example of this is
going to be
filter
and create a values 2 here
and we're just going to do this
[Music]
and we're going to add
a couple of empty strings
and we're going to essentially the same
thing so for this time we're going to do
filter
use values 2 and we're going to call it
it's blank
and let's call his blank first
so let's just do print
is blank and we're going to look at
these we're going to do
uh this guy and then we're going to do
them again
[Music]
let's see let's see for this one's going
to be an example of
not blank
and we're going to do an example of
his blank so
let's play around with this one a little
bit so let's go take a look at that
and let's see what we come up with
so here uh let's see so the example
filter
doesn't do anything so example of not
blank so we're gonna we have something
here oh
i'm sorry that's not what we want so
let's just do uh equals uh
not oops
x did i do that right before
oh let's do this that may give me what i
want
except for let's try it
okay so let's go play with this for a
second
there we go okay i'll just use the right
variable
so uh not blank gives us a false
if it is blank it gives us a true i
guess since that's a print i don't need
that
so now what we can see here
is this is filtering based on true so
you can see here we did
if it's blank then it's going to keep it
so we would say
so if we switch it and go to not blank
and then uh just reverse that
well let's just do this uh if it is
greater than zero then it must be not
blank
and we're gonna see that oops
ah where did i do that
oh sorry i have to change the name here
and once i change it
make sure i save it let's clear it just
because
and go here and so now
notice i still get i get a filter object
instead of a map object
but i can still do a list on this and
now
i'm just filtering out everything that
is not blank
so that gives me a nice little
you know way to go through a list of
some sort
and filter it out no pun intended based
on that name
so now similarly now let's do
sorted next and let's just do
uh we'll do sorted based on values
let's see so let's go
oh okay i just had something weird going
there we go
[Music]
okay well my keyboard has got something
stuck there
okay so example of
sorted and i'm going to do our results
equals let's do this here
wow i've got some weird stuff going on
here
[Music]
let's try this again
all right well let's do this
[Music]
wow that is some craziness
let's do this
okay so now let's go back to that
okay so example sorted
uh i just want to do it like i did with
the map
so let's do those three except for
sorted is not going to take anything but
the array
results results okay and so now if we go
back here
clear it
and run it we'll see oh let me that's
let's do reversed well let's do this
let's move these around a little bit
so let's 2 2 6
four ten eight
and now
we can see that sorted is going to give
us that sorted value
and notice here that the results is
actually
a list so sorta is going to return to
list if i want to do reversed
i can do
[Music]
i'm just gonna do it that way
and reversed how however gives us a
reversed object
[Music]
and so we have to go look at it
and see that in this case it's not
this is important there's two things
here sort is probably the easiest or
sorry sorted
is the easiest to deal with as far as it
returns you
a list reversed which you would think
does sort but it's reversed you can
actually do
let's do sorted i'm going to reverse it
and i want it to be uh reverse
[Applause]
[Music]
equals true
so i think that's a capital t
that so now we'll see
here it is reversed so i've got sort and
i've got it sort of reversed ordered
what reverse does is not
related to sorting the values but just
the keys themselves so we can see our
original was
uh here 2 4 oh i'm sorry uh where'd it
go
oh i don't have it so our original
up here was 2 6 4 10 8.
if we look it's 2 6 4 10 8 reversed
so it's reversed the order of
the items within it not the sort order
if you want to reverse a sort order
then you have to do reverse equals true
and that leaves us one left which is
reduced
and i can actually do that sort of
similar to what i had before
where i go here we go add one okay so
now what reduce does is
it's going to take
[Music]
it's going to apply the function
oh sorry reduce is
actually in funk tools so let's see if
that
i may have to import that so let's see
just a second so
although you will see it probably on the
certification exam
so what i do here is i'm going to give
it the values but what it's going to do
it's going to add the function but then
it's going to keep
applying uh additional stuff to it so
when i do add one then it's just going
to throw in the next one and it's
eventually going to get it down
it's going to take the results and you
typically well i'm sorry
i can't do that here as i'm thinking
about it i actually need
i'm just going to call it
let's call it add vowel and this one's
going to take an x
and a y uh because
uh return x plus y what it does
with reduce uh let's see that work blah
blah blah let's get that there return x
plus y that looks good
and then uh reduce is going to reduce
add when i call it add one
no add val
and what it's going to do is it's going
to go through and
take the first two items because of
those parameters
in this case it'll take uh so 2 and 6.
so it's going to add those together and
give you eight but then it's going to
take that result
and then tack on the next one so it's
going to call advil again
with eight and let me do this
called with
and i'm just going to do
a string
x and let's do it this way
and string y and we will see
that it's going to add those things up
and it's going to keep tacking values on
and using the result as the first value
as the first parameter
so if we go back here let's clear it out
because it's going to give us a bunch of
stuff
and we'll see here so first it comes in
with two and six it adds those together
so it does two and six adds those to get
four it does eight and four
adds it together get 12. then it calls
12 with the 10. close that together get
22 cause
22 with 8 and boom we end up with 30
which is uh here
and we're going to find in this case
because it returns that final value
which is an integer
we get an error because we can't do a
list on an integer it's not iterable
as they say you can't go across it
so that gives us a couple
of and that's a few very valuable ways
to deal with sets
of some sort to deal with arrays in
particular values we can use our filters
to reduce stuff out to
to filter stuff out we can use map to
just call
you know use that function against every
item within the collection within the
array
we can do reduce to continually call
that thing uh that to continually do
that
function and then feed its result
back in and start working our way down
the list of parameters we can do
reversed remember reverse reverses the
items in the array which is not the same
as reversing the sort
uh the sort order we can use sorted to
sort them and we can do sorted with
where we have here and add reverse
equals true
to send those in reverse value
that's uh that's pretty good set of
stuff today so
we'll let it get to it as always you can
find stuff in the
github github repository and uh
stick around next time we will just
continue working our way through these
we're actually getting pretty close
so we're getting close to the point
where if you've been following through
with this you
probably can be in pretty good shape to
go take a stab at a certification
that being said let's get to it so go
out there and have yourself a great day
a great week and we will talk to you
next time
you