📺 Develpreneur YouTube Episode

Video + transcript

Python Certification Training - List Functions Part 2

2021-07-22 •Youtube

Detailed Notes

We cover the second part of two tutorials on the list functions as we look toward Python certification

Useful Links: https://www.w3schools.com/python/python_ref_list.asp https://www.geeksforgeeks.org/python-string-rfind/ https://www.w3schools.com/python/ref_func_sorted.asp

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]
hello and welcome back
we're continuing our season our series
where we talk about python certification
in this episode we are continuing uh
from our prior episode if you remember
we were working on pardon me while i
adjust my mic just a little bit there we
go maybe okay sorry
three two one
and so last time we talked about strings
uh string functions
and we just got not really started we
got a pretty good way into them but now
we're going to
continue and we're going to start with
the sort command so the first thing we
do
with this one is because we've got so
many in this example if you sort of see
we've got a lot of stuff going on so i'm
going to jump to the end here and
actually let me just do this
just to help us with our eyeballs as we
go
through this um
we'll do something like that
and so we're gonna we've got a new list
we're gonna work with because this is
gonna be
uh needed for some of the items we're
gonna work on here so our example now is
gonna be red green blue
red green blue white black so it's just
a bunch of strings they just happen to
be
color names so first thing we do is sort
on a very simple sort uh did i say that
and so we come down here and we sort
that here's the original
list and now we can see that we sort it
now it sorts the items are black blue
blue green green red red white
and so it is alphabetically sorting
everything
bla comes before blue
and so sor there's some there are a few
features we can do a sort that we're not
really gonna
mess with too much uh just because
um i don't think you're gonna see them
uh but essentially you can do some sort
based on a couple things that
well the one we will show basically you
can do a custom sort
before i jump into that let me do
where's that
list i'm gonna do
so as an example for sort this is going
to be
for reverse which as you will see
is somewhat similar and it's important
to note that
the sort and reverse actually
impact the list itself
the output for these is nothing but it
does change
the list itself so now if we look at
list reverse
um yeah i'll do it this way first
so if you look at list reverse it's
easiest here because we can see here's
what it was after we sorted it
alphabetically
and now if you look it went white red
red green green blue blue black
exactly the reverse of what it did so
all reverse does is it swaps the items
or it reverses the order
so that one's pretty easy uh now sort
get a little more advanced example
because you probably will come across it
in this situation what i do is i go in
and i'm just going to create this little
function
all it does is you give it a value and
it's going to turn the length of the
value
and then let's see
i can come down here and let me
where did it go let me go back to
let's just do this just so i can save
the state of it
and then right before i get into that
i'm going to say examples equals example
2 which is basically so i can just reset
to this
this top setting
[Music]
and now what i'm going to do this time
is i'm going to sort but i'm going to
give it a key
and that key is this length function
that i defined so what it's going to do
is it's going to call this function
for every item and it's going to return
the link so now we're going to see
when we sort based on that key
and it should be fairly obvious that we
see that now
it sorts the items of link 3
length 4 length five
and it just goes through and it just
sorts them that way so there are ways
that you can do
custom sorts that you can do some custom
adjustments to these things
and um and get something actually you
know fairly complicated particularly you
would use this if you're doing so it's
not like a simple list like a string or
numbers but maybe if you were
had a string or sorry a list of objects
you know or classes or some chickens
would be objects that you were working
with
and they may have their own essentially
from another language you may hear about
it as a comparator of some sort that
says
uh maybe i'm sorting by id values or
some calculated number
something like that so sort can get
pretty cool uh sorted
is let's just go here
[Music]
let's do this
and this
so sorted is actually
a little bit different approach if we
wanted to do so here example.sort
gives us uh does not return anything but
it does change
the uh the array itself
and so let's see so here so i did that
let's go back and reset
and so if we do
let's do this
so let's do first we're going to do
the sorted example
and then we're just going to print
example
and if you look at that now you're going
to see
here um let me
put something here to make this a little
easier
let's do it this way and we'll do sorted
list
[Music]
so initially we do it if we send it
sorted
see so if we actually kick out the
output of sorted
then it is now that sorted list much as
we saw up here
but if we actually grab the list it goes
back to what we originally
what our example is here so it's not
the same anymore which we did sort that
at some point
apparently
what do we do to example two
well somewhere i got that sorted anyways
so now you see that's not actually i'm
not sure how i messed
this one up but well let's see so we got
in a weird little state so let's just do
this
just to be safe
there we go let's move that up
so i think i messed it up there we go
okay
somewhere i must have screwed up my
example two somewhere all right so
sorted
actually gives you back a sorted list
sort sorts the list itself
so it's sort of important to know
particularly as you're playing around
with things there
let's do extend so extend we had an
example
way back here here we go
if you remember when we did append
what it did is if we added um
let's do this let's grab that one again
because i'm going to do these so this is
going to be for our pinned
back here
me disappend
i'm sorry extend plus i didn't even
type that right so i do extend
i'm going to look at these two so if i
did colors in a pen and then i wanted to
print colors
so first i want to do a pen and then i'm
going to do the same thing i'm going to
reset it
and i'm going to do extend and this
should be the easiest way to see the
difference
in these two so if you see here this
first one
where i do the extend i'm sorry when i
do the append
it takes that array and just puts a it
adds an object which is the array it
adds that to the end
but when i do extend
it assumes there's going to be a list of
items and it just builds it into the
array so you see where the
brackets start to identify that this is
an item within our array
and over here there is not that it takes
all of those individual items
and extends them in
this effectively is your way to
concatenate a pair of
a pair of like you know lists or arrays
or things like that
is that you can just come in and you
know start doing them from an extend and
it will
uh paste those two together for you
essentially so next i want to do
is uh length let's uh just keep it
simple
if i do the
length of i'm sorry
colors dot and then
oh it's the line of colors uh one two
three we're going to see that
that last thing is it's going to give us
a link i think we've done this before
we may not have um but basically it's
going to count
here so we've got red green blue white
black if we had this guy
instead oh we can probably do that
so if we do example and actually let's
make sure i
didn't screw that up somewhere
so let's come down here into example
that's going to be one two three four
five six seven
eight yep there we go there's our eight
that's pretty straightforward and then
the one of the last ones i want to do
is uh sort of go back because i think we
covered
in and not in but we can do if
let's do if red
in colors
i'm just going to do print
it's in there
and actually let's pick what's that
[Music]
and let's do if purple
not in colors it's not in there
and so this is just a simple logic
and we'll see it's in there it's not in
there so here
we can actually look for red now if we
change it to
let's say just an e then you're going to
find that it's not in there
well it's in there doesn't show up it's
looking for this exact item so in
and not in are going to be based on the
item uh items within the array so it's
going to see if we can find them
now if we were to do well for example
red's in there twice but no
it doesn't doesn't care it's just it
either is
or it is not now last thing i wanted to
do
was uh one other that's a string so if i
do
let's do my my string because i sort of
missed this earlier
and want to make sure i swing back
around to it so if my string equals
lazy dog jumps over log
and i want to do a find
so this is a find example where i'm
going to put that somewhere
there we go
[Music]
so when i do find and i'm going to do
our find
and so if i do
let's do my string dot
oops
fine and let me find
um something let's see i'm not sure yet
what i'm gonna do
so let's do um let's do oh
[Music]
and so between find and are fine it's
going to be looking for o within here
let's see what it finds
it does six
and it oops that's not in our fine and
it does that's
there we go 6 and 21. and so
oh good and here's a good example
because we do have three o's in here so
if you find
0 1 2 3 4 5 6.
so find and that's what happens gives
you the first occurrence
so if you want to do sometimes you see
this is like a pos or position or
something like that function
the r fine actually starts at the end
and finds the last occurrence which is
zero one two three
four five six seven eight nine 10 11 12
13 14 15 16 17 18 19 20 21
which is what we got so we get our 21
down there
and it skips over this one fine gets the
first one
are fine get you the last one
and i think i'm gonna wrap that up for
this time around because i threw the i
did throw those strings in and actually
i think i'm gonna probably move these
because i was just as i was looking
through this
i did swing back around so i'm going to
throw those in
fine throw that in here
and that'll work
so that'll be updated in the code
repository
and that covers uh really covers a lot
of what we've got in the world of
arrays i would um
i would go back through these a little
bit and just play around with them
so you you have an understanding of what
they are in particular
the um the things that deal with indexes
of some sort
uh that tell you you know like index and
fine or not
but index and um
length and uh those kinds of things just
to see
what's there uh you may want to look
into the i don't think you need anything
complicated in the sorted
like i did you know probably just to
know that you can do a key
uh you may want to play around with that
just from you know feeling comfortable
when we get to that certification point
and that will this that'll do it for uh
this one i don't think we need to go
very further in it we'll
get a good break here in in topics we'll
switch over to something else next time
around
uh but until then go out there have
yourself a great day a great week
and we will talk to you next time
you
Transcript Segments
0.46

[Music]

26.16

hello and welcome back

27.68

we're continuing our season our series

30.48

where we talk about python certification

33.2

in this episode we are continuing uh

35.68

from our prior episode if you remember

37.76

we were working on pardon me while i

39.36

adjust my mic just a little bit there we

42.64

go maybe okay sorry

45.84

three two one

50

and so last time we talked about strings

52.719

uh string functions

53.92

and we just got not really started we

56.079

got a pretty good way into them but now

57.6

we're going to

58.559

continue and we're going to start with

61.92

the sort command so the first thing we

65.28

do

66.159

with this one is because we've got so

68

many in this example if you sort of see

69.92

we've got a lot of stuff going on so i'm

71.439

going to jump to the end here and

72.72

actually let me just do this

75.92

just to help us with our eyeballs as we

78.64

go

79.04

through this um

83.04

we'll do something like that

86.64

and so we're gonna we've got a new list

88

we're gonna work with because this is

89.439

gonna be

90.079

uh needed for some of the items we're

91.6

gonna work on here so our example now is

93.68

gonna be red green blue

94.799

red green blue white black so it's just

96.64

a bunch of strings they just happen to

98.72

be

99.2

color names so first thing we do is sort

102.88

on a very simple sort uh did i say that

108.24

and so we come down here and we sort

110.32

that here's the original

113.2

list and now we can see that we sort it

115.92

now it sorts the items are black blue

118

blue green green red red white

120.24

and so it is alphabetically sorting

122.399

everything

123.36

bla comes before blue

127.759

and so sor there's some there are a few

129.679

features we can do a sort that we're not

131.28

really gonna

131.92

mess with too much uh just because

135.28

um i don't think you're gonna see them

139.2

uh but essentially you can do some sort

141.04

based on a couple things that

142.879

well the one we will show basically you

144.319

can do a custom sort

146.16

before i jump into that let me do

148.8

where's that

149.92

list i'm gonna do

153.519

so as an example for sort this is going

156.319

to be

156.8

for reverse which as you will see

161.92

is somewhat similar and it's important

165.2

to note that

166.239

the sort and reverse actually

169.36

impact the list itself

173.28

the output for these is nothing but it

175.68

does change

176.72

the list itself so now if we look at

178.8

list reverse

181.04

um yeah i'll do it this way first

186.239

so if you look at list reverse it's

188.4

easiest here because we can see here's

190.08

what it was after we sorted it

191.28

alphabetically

192.8

and now if you look it went white red

195.76

red green green blue blue black

198.319

exactly the reverse of what it did so

200.239

all reverse does is it swaps the items

202.64

or it reverses the order

205.84

so that one's pretty easy uh now sort

209.2

get a little more advanced example

210.72

because you probably will come across it

213.599

in this situation what i do is i go in

215.2

and i'm just going to create this little

216.48

function

217.76

all it does is you give it a value and

219.76

it's going to turn the length of the

220.799

value

222

and then let's see

225.04

i can come down here and let me

230.159

where did it go let me go back to

235.439

let's just do this just so i can save

238.159

the state of it

241.439

and then right before i get into that

243.2

i'm going to say examples equals example

245.599

2 which is basically so i can just reset

247.68

to this

249.12

this top setting

251.61

[Music]

252.72

and now what i'm going to do this time

254.48

is i'm going to sort but i'm going to

255.84

give it a key

257.68

and that key is this length function

259.84

that i defined so what it's going to do

261.68

is it's going to call this function

263.919

for every item and it's going to return

266.16

the link so now we're going to see

268.08

when we sort based on that key

272.08

and it should be fairly obvious that we

274.32

see that now

275.52

it sorts the items of link 3

278.88

length 4 length five

282.56

and it just goes through and it just

283.84

sorts them that way so there are ways

286.16

that you can do

287.04

custom sorts that you can do some custom

289.68

adjustments to these things

291.52

and um and get something actually you

293.6

know fairly complicated particularly you

296

would use this if you're doing so it's

297.36

not like a simple list like a string or

299.12

numbers but maybe if you were

301.199

had a string or sorry a list of objects

304.32

you know or classes or some chickens

306.08

would be objects that you were working

307.36

with

308.16

and they may have their own essentially

310.4

from another language you may hear about

311.759

it as a comparator of some sort that

313.84

says

314.88

uh maybe i'm sorting by id values or

317.52

some calculated number

318.8

something like that so sort can get

322

pretty cool uh sorted

325.919

is let's just go here

330.37

[Music]

331.44

let's do this

334.88

and this

340.72

so sorted is actually

344.08

a little bit different approach if we

345.68

wanted to do so here example.sort

348.96

gives us uh does not return anything but

352

it does change

353.039

the uh the array itself

356.08

and so let's see so here so i did that

359.52

let's go back and reset

363.039

and so if we do

368.319

let's do this

371.84

so let's do first we're going to do

375.52

the sorted example

378.639

and then we're just going to print

379.52

example

382.479

and if you look at that now you're going

385.52

to see

386.08

here um let me

389.44

put something here to make this a little

392.84

easier

396.72

let's do it this way and we'll do sorted

400.08

list

401.27

[Music]

404.319

so initially we do it if we send it

407.6

sorted

408.24

see so if we actually kick out the

410.08

output of sorted

412

then it is now that sorted list much as

414.16

we saw up here

416.639

but if we actually grab the list it goes

419.84

back to what we originally

421.039

what our example is here so it's not

424.4

the same anymore which we did sort that

427.44

at some point

428.16

apparently

433.44

what do we do to example two

438.72

well somewhere i got that sorted anyways

444.56

so now you see that's not actually i'm

446.88

not sure how i messed

448.08

this one up but well let's see so we got

449.919

in a weird little state so let's just do

451.36

this

453.599

just to be safe

461.68

there we go let's move that up

469.12

so i think i messed it up there we go

471.52

okay

472.319

somewhere i must have screwed up my

475.36

example two somewhere all right so

478.479

sorted

479.36

actually gives you back a sorted list

481.52

sort sorts the list itself

484.08

so it's sort of important to know

485.759

particularly as you're playing around

487.039

with things there

488.879

let's do extend so extend we had an

491.68

example

492.479

way back here here we go

495.68

if you remember when we did append

499.28

what it did is if we added um

503.84

let's do this let's grab that one again

507.84

because i'm going to do these so this is

509.84

going to be for our pinned

516.32

back here

520

me disappend

525.36

i'm sorry extend plus i didn't even

529.279

type that right so i do extend

534

i'm going to look at these two so if i

535.279

did colors in a pen and then i wanted to

536.959

print colors

541.2

so first i want to do a pen and then i'm

543.68

going to do the same thing i'm going to

544.88

reset it

546

and i'm going to do extend and this

548.8

should be the easiest way to see the

550.32

difference

550.8

in these two so if you see here this

553.279

first one

553.92

where i do the extend i'm sorry when i

556

do the append

557.36

it takes that array and just puts a it

559.2

adds an object which is the array it

562.08

adds that to the end

564.24

but when i do extend

567.44

it assumes there's going to be a list of

568.8

items and it just builds it into the

570.88

array so you see where the

572.24

brackets start to identify that this is

574.08

an item within our array

575.76

and over here there is not that it takes

578.24

all of those individual items

579.68

and extends them in

583.519

this effectively is your way to

586.32

concatenate a pair of

589.12

a pair of like you know lists or arrays

590.959

or things like that

592.24

is that you can just come in and you

594

know start doing them from an extend and

596.08

it will

597.12

uh paste those two together for you

598.88

essentially so next i want to do

601.12

is uh length let's uh just keep it

604.72

simple

608.24

if i do the

612.88

length of i'm sorry

616.56

colors dot and then

621.2

oh it's the line of colors uh one two

623.92

three we're going to see that

626.48

that last thing is it's going to give us

627.92

a link i think we've done this before

631.04

we may not have um but basically it's

634.48

going to count

635.36

here so we've got red green blue white

637.04

black if we had this guy

639.2

instead oh we can probably do that

642.32

so if we do example and actually let's

645.6

make sure i

646.88

didn't screw that up somewhere

649.92

so let's come down here into example

656.32

that's going to be one two three four

658.399

five six seven

659.519

eight yep there we go there's our eight

663.839

that's pretty straightforward and then

665.519

the one of the last ones i want to do

668

is uh sort of go back because i think we

670.079

covered

671.519

in and not in but we can do if

676.8

let's do if red

680.24

in colors

685.68

i'm just going to do print

689.04

it's in there

693.519

and actually let's pick what's that

704.84

[Music]

708.24

and let's do if purple

711.6

not in colors it's not in there

715.12

and so this is just a simple logic

719.2

and we'll see it's in there it's not in

720.959

there so here

722.399

we can actually look for red now if we

724.72

change it to

725.519

let's say just an e then you're going to

728

find that it's not in there

729.44

well it's in there doesn't show up it's

732.959

looking for this exact item so in

736.399

and not in are going to be based on the

737.92

item uh items within the array so it's

740.56

going to see if we can find them

741.839

now if we were to do well for example

743.839

red's in there twice but no

745.44

it doesn't doesn't care it's just it

747.839

either is

748.88

or it is not now last thing i wanted to

752.399

do

753.44

was uh one other that's a string so if i

756.24

do

757.04

let's do my my string because i sort of

760.16

missed this earlier

762.24

and want to make sure i swing back

763.76

around to it so if my string equals

766.48

lazy dog jumps over log

773.76

and i want to do a find

777.44

so this is a find example where i'm

779.92

going to put that somewhere

781.839

there we go

786.85

[Music]

789.12

so when i do find and i'm going to do

791.36

our find

796.48

and so if i do

802.56

let's do my string dot

806.32

oops

811.279

fine and let me find

814.959

um something let's see i'm not sure yet

818.24

what i'm gonna do

821.44

so let's do um let's do oh

828.28

[Music]

831.04

and so between find and are fine it's

833.279

going to be looking for o within here

834.639

let's see what it finds

838.639

it does six

841.68

and it oops that's not in our fine and

843.76

it does that's

845.199

there we go 6 and 21. and so

849.279

oh good and here's a good example

850.32

because we do have three o's in here so

851.839

if you find

852.48

0 1 2 3 4 5 6.

856.24

so find and that's what happens gives

858.24

you the first occurrence

860.399

so if you want to do sometimes you see

861.92

this is like a pos or position or

863.68

something like that function

865.6

the r fine actually starts at the end

867.92

and finds the last occurrence which is

869.92

zero one two three

871.36

four five six seven eight nine 10 11 12

874.16

13 14 15 16 17 18 19 20 21

877.839

which is what we got so we get our 21

880.639

down there

882.56

and it skips over this one fine gets the

885.199

first one

886.24

are fine get you the last one

889.839

and i think i'm gonna wrap that up for

891.36

this time around because i threw the i

892.72

did throw those strings in and actually

894.72

i think i'm gonna probably move these

896.88

because i was just as i was looking

898.32

through this

898.959

i did swing back around so i'm going to

901.12

throw those in

902.16

fine throw that in here

910.959

and that'll work

914.24

so that'll be updated in the code

915.839

repository

917.44

and that covers uh really covers a lot

920.16

of what we've got in the world of

922.32

arrays i would um

925.6

i would go back through these a little

926.88

bit and just play around with them

928.72

so you you have an understanding of what

930.88

they are in particular

933.6

the um the things that deal with indexes

936.32

of some sort

937.839

uh that tell you you know like index and

940.16

fine or not

941.279

but index and um

944.959

length and uh those kinds of things just

948

to see

948.72

what's there uh you may want to look

950.8

into the i don't think you need anything

952.32

complicated in the sorted

954.32

like i did you know probably just to

955.839

know that you can do a key

957.759

uh you may want to play around with that

959.759

just from you know feeling comfortable

961.759

when we get to that certification point

963.759

and that will this that'll do it for uh

966.079

this one i don't think we need to go

967.44

very further in it we'll

968.639

get a good break here in in topics we'll

970.399

switch over to something else next time

972.079

around

973.199

uh but until then go out there have

974.959

yourself a great day a great week

977.279

and we will talk to you next time

995.68

you