📺 Develpreneur YouTube Episode

Video + transcript

Python Certification Training - Map,Reduce,Filter,Sorted

2021-08-24 •Youtube

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
1.32

[Music]

26.24

well hello

26.96

and welcome back we are continuing

28.96

looking at our python certification

31.599

uh series of tutorials this episode

34.32

we're going to look at

35.52

what are called essentially higher order

37.44

functions now we're going to look at map

39.84

filter reduce reversed and sorted

43.44

these are good followers uh follow-ons

45.92

to the uh

46.719

the whole lambda concept because it gets

49.68

into the idea

50.879

of um sort of recursion and some things

54.719

like that that just

56.399

i think once you unders you've got a

58.32

rough feel for lambdas then these kinds

60.16

of things make sense or maybe vice versa

62.239

these may help you understand

64.239

a lambda a little bit better the first

66.96

one i'm going to look at

67.92

is map so let me just do this

77.6

and the way map works

80.88

is that you give it two parameters

83.92

let's see here so what we're going to do

85.68

is we're going to use this add one

87.2

that we used before we had this add one

89.759

method

90.96

function actually and all it does is it

93.6

just returns

94.88

uh whatever value you send it and it

96.64

adds one to it and then returns it

98.88

so what we're doing here is we're going

100.799

to call add1

102.64

and we're going to send it an array and

105.6

that's what map does is it says

107.439

take this function and then walk through

109.84

the array and then just call the

111.119

function with each of those values

113.04

so what we're going to see here and let

115.2

me go ahead

116.159

and do that let's clear this

119.439

up

123.68

and so the first thing we do let's flip

127.119

back over here

129.52

is we store the result in the sinkhole's

131.76

results

132.72

now if you look at results itself if you

136.4

try to present it

138.16

print it it's going to return a map

140.959

object so you actually get an

142.72

object back you get this essentially

144.64

this collection of results

147.2

so we can do is we can just call list

150.319

and i care if we've seen this before but

152.319

basically all this does it says

154.64

take this collection and turn it into a

157.12

list

157.92

and we can print that out and we can see

160.16

that when we send 2 4 6 8 10

162.8

it gives us back 3 5 7 9 and 11.

166.879

so we've got an easy way to apply a

169.519

function across a series of stuff

172.879

now similarly

177.76

let's create one that's going to be

180.959

let's do is ant

187.76

well let's call it is gnome

192.239

oh let's just call actually let's get

193.68

this one really good let's call it is

195.519

blank

196.48

and it's going to take x which is going

198.4

to be a string

200.48

and it's going to return a

203.519

boolean and it's going to say

207.44

uh it's just going to be let's say

210.84

return

214.56

let's say uh

218.4

let's do

221.68

length of that

226.72

less than one so

232.239

well let's do it this way let's clean it

234

up a little bit

243.28

and then return results just in case

247.2

i have an issue with that

251.439

and it probably doesn't like this let me

253.04

look at

255.36

this is that maybe what it wants

258.959

because it's probably complaining that's

260.32

not a boolean

267.12

oh sorry

269.01

[Music]

272.32

there so now if i just do

278.96

let's first do this example of this is

281.68

going to be

282.16

filter

286.479

and create a values 2 here

292.24

and we're just going to do this

296.74

[Music]

301.28

and we're going to add

305.12

a couple of empty strings

312.56

and we're going to essentially the same

313.84

thing so for this time we're going to do

316.56

filter

319.84

use values 2 and we're going to call it

322.32

it's blank

332.08

and let's call his blank first

335.6

so let's just do print

339.12

is blank and we're going to look at

342.24

these we're going to do

343.199

uh this guy and then we're going to do

346.72

them again

353.35

[Music]

357.039

let's see let's see for this one's going

358.639

to be an example of

362.56

not blank

370.8

and we're going to do an example of

374.08

his blank so

379.039

let's play around with this one a little

380.24

bit so let's go take a look at that

382.319

and let's see what we come up with

385.36

so here uh let's see so the example

388.96

filter

389.52

doesn't do anything so example of not

390.72

blank so we're gonna we have something

392.96

here oh

393.52

i'm sorry that's not what we want so

396.96

let's just do uh equals uh

400

not oops

404

x did i do that right before

408.72

oh let's do this that may give me what i

413.199

want

419.759

except for let's try it

424.96

okay so let's go play with this for a

426.4

second

428.08

there we go okay i'll just use the right

431.28

variable

432.08

so uh not blank gives us a false

435.28

if it is blank it gives us a true i

438

guess since that's a print i don't need

439.36

that

441.36

so now what we can see here

444.8

is this is filtering based on true so

447.68

you can see here we did

449.12

if it's blank then it's going to keep it

452.96

so we would say

456.639

so if we switch it and go to not blank

460.08

and then uh just reverse that

463.199

well let's just do this uh if it is

466.319

greater than zero then it must be not

469.84

blank

471.68

and we're gonna see that oops

481.12

ah where did i do that

484.4

oh sorry i have to change the name here

494.72

and once i change it

497.84

make sure i save it let's clear it just

501.599

because

502.639

and go here and so now

506.08

notice i still get i get a filter object

508

instead of a map object

510

but i can still do a list on this and

512.959

now

514.159

i'm just filtering out everything that

516

is not blank

517.599

so that gives me a nice little

521.2

you know way to go through a list of

524.08

some sort

525.279

and filter it out no pun intended based

529.2

on that name

530.48

so now similarly now let's do

533.68

sorted next and let's just do

537.92

uh we'll do sorted based on values

543.44

let's see so let's go

547.2

oh okay i just had something weird going

550.8

there we go

555.48

[Music]

556.88

okay well my keyboard has got something

559.68

stuck there

562.72

okay so example of

569.12

sorted and i'm going to do our results

573.279

equals let's do this here

580.24

wow i've got some weird stuff going on

583.92

here

586.75

[Music]

589.2

let's try this again

594.48

all right well let's do this

595.87

[Music]

599.519

wow that is some craziness

610.24

let's do this

614.24

okay so now let's go back to that

619.04

okay so example sorted

622.24

uh i just want to do it like i did with

624

the map

625.68

so let's do those three except for

631.839

sorted is not going to take anything but

634.959

the array

637.44

results results okay and so now if we go

640.8

back here

641.519

clear it

645.6

and run it we'll see oh let me that's

651.92

let's do reversed well let's do this

653.839

let's move these around a little bit

655.76

so let's 2 2 6

659.44

four ten eight

664.399

and now

667.44

we can see that sorted is going to give

669.519

us that sorted value

671.76

and notice here that the results is

674.48

actually

676.88

a list so sorta is going to return to

678.64

list if i want to do reversed

688.72

i can do

690.83

[Music]

694.72

i'm just gonna do it that way

700.24

and reversed how however gives us a

703.519

reversed object

705.13

[Music]

707.519

and so we have to go look at it

710.72

and see that in this case it's not

713.839

this is important there's two things

715.2

here sort is probably the easiest or

717.76

sorry sorted

719.36

is the easiest to deal with as far as it

722.079

returns you

724.24

a list reversed which you would think

729.04

does sort but it's reversed you can

731.44

actually do

734.959

let's do sorted i'm going to reverse it

744.959

and i want it to be uh reverse

748.68

[Applause]

749.29

[Music]

750.72

equals true

758.32

so i think that's a capital t

762.32

that so now we'll see

765.44

here it is reversed so i've got sort and

768.079

i've got it sort of reversed ordered

769.839

what reverse does is not

773.12

related to sorting the values but just

775.36

the keys themselves so we can see our

777.36

original was

779.68

uh here 2 4 oh i'm sorry uh where'd it

782.56

go

785.44

oh i don't have it so our original

788.48

up here was 2 6 4 10 8.

792.32

if we look it's 2 6 4 10 8 reversed

795.92

so it's reversed the order of

799.12

the items within it not the sort order

801.519

if you want to reverse a sort order

803.6

then you have to do reverse equals true

809.519

and that leaves us one left which is

812.839

reduced

815.68

and i can actually do that sort of

817.839

similar to what i had before

819.44

where i go here we go add one okay so

824.56

now what reduce does is

828.24

it's going to take

831.12

[Music]

832.639

it's going to apply the function

838.72

oh sorry reduce is

841.839

actually in funk tools so let's see if

844.48

that

845.04

i may have to import that so let's see

846.88

just a second so

849.199

although you will see it probably on the

852

certification exam

854.399

so what i do here is i'm going to give

856.32

it the values but what it's going to do

858.32

it's going to add the function but then

860.88

it's going to keep

862.72

applying uh additional stuff to it so

865.76

when i do add one then it's just going

867.12

to throw in the next one and it's

868.24

eventually going to get it down

870.079

it's going to take the results and you

872.32

typically well i'm sorry

874.48

i can't do that here as i'm thinking

876.16

about it i actually need

879.04

i'm just going to call it

882.16

let's call it add vowel and this one's

885.04

going to take an x

886.48

and a y uh because

890.399

uh return x plus y what it does

893.839

with reduce uh let's see that work blah

896.48

blah blah let's get that there return x

898.399

plus y that looks good

900.399

and then uh reduce is going to reduce

904

add when i call it add one

908.48

no add val

913.839

and what it's going to do is it's going

915.04

to go through and

917.199

take the first two items because of

918.8

those parameters

920.8

in this case it'll take uh so 2 and 6.

924.8

so it's going to add those together and

926

give you eight but then it's going to

927.36

take that result

928.72

and then tack on the next one so it's

930.56

going to call advil again

932.48

with eight and let me do this

938.72

called with

943.44

and i'm just going to do

946.639

a string

949.839

x and let's do it this way

954.959

and string y and we will see

958

that it's going to add those things up

959.759

and it's going to keep tacking values on

961.759

and using the result as the first value

964.24

as the first parameter

967.759

so if we go back here let's clear it out

970.72

because it's going to give us a bunch of

971.68

stuff

972.959

and we'll see here so first it comes in

974.72

with two and six it adds those together

977.199

so it does two and six adds those to get

978.959

four it does eight and four

980.56

adds it together get 12. then it calls

982.24

12 with the 10. close that together get

984.32

22 cause

985.12

22 with 8 and boom we end up with 30

989.759

which is uh here

993.04

and we're going to find in this case

994.32

because it returns that final value

996.8

which is an integer

997.92

we get an error because we can't do a

999.44

list on an integer it's not iterable

1002.72

as they say you can't go across it

1007.36

so that gives us a couple

1010.399

of and that's a few very valuable ways

1013.6

to deal with sets

1015.04

of some sort to deal with arrays in

1018.16

particular values we can use our filters

1020.24

to reduce stuff out to

1022.16

to filter stuff out we can use map to

1024.88

just call

1025.679

you know use that function against every

1027.36

item within the collection within the

1029.199

array

1030.079

we can do reduce to continually call

1033.679

that thing uh that to continually do

1036.799

that

1037.52

function and then feed its result

1041.199

back in and start working our way down

1042.959

the list of parameters we can do

1045.679

reversed remember reverse reverses the

1049.039

items in the array which is not the same

1051.44

as reversing the sort

1053.36

uh the sort order we can use sorted to

1056.48

sort them and we can do sorted with

1059.76

where we have here and add reverse

1062.08

equals true

1063.2

to send those in reverse value

1066.4

that's uh that's pretty good set of

1068.24

stuff today so

1069.679

we'll let it get to it as always you can

1072

find stuff in the

1073.36

github github repository and uh

1077.2

stick around next time we will just

1078.48

continue working our way through these

1080.08

we're actually getting pretty close

1082.24

so we're getting close to the point

1083.36

where if you've been following through

1084.4

with this you

1085.28

probably can be in pretty good shape to

1087.76

go take a stab at a certification

1090.72

that being said let's get to it so go

1092.559

out there and have yourself a great day

1094

a great week and we will talk to you

1096.799

next time

1113.2

you