📺 Develpreneur YouTube Episode

Video + transcript

Python Certification Training - List Functions Part 1

2021-07-20 •Youtube

Detailed Notes

We cover the first 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

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 our series of python
certification
examples as we walk through the
python certification series for the
certified associate in python
programming walking through a lot of
these
actually we're going to eventually walk
through all of these items to some
extent go through some examples
this episode we are going to look at
list functions
now we've worked with lists before and
this is going to be
part one because there's a lot to go
over
so this time we're just going to take
our first stab at it and then next
episode we'll come through and get the
the second part get the rest of those
those functions
we've created some lists before uh i'm
going to probably stick with the
colors and we've also added an example
list
and really what we're doing here because
of some of the examples is we're going
to see where
our initial one was red green blue and
white black the second one
actually has got blue white and black
so we have those last three duplicated
so we can actually see duplicates
it's not a bunch of unique values it
actually has more than
one instance of some values which is
going to become important
for some of the makes it useful for some
of the functions we're going to look at
the first one we'll look at is append
and now this one it's pretty
straightforward so you take we're going
to work off this
colors array and we're just going to go
in and we're going to say colors append
and then we're going to give it the
string
yellow so let's see how that works out
so if we do it we see where our initial
list started out was red green blue
white black
we append yellow and then yellow as you
would expect
gets appended now an interesting thing
we've looked before i think there's a
point in the past where we did an
example where we had
we tried to append an array
and so if we try to add purple and pink
so let's do that so we got a yellow and
we're going to turn right around and add
purple and pink
what we see now is yellow
is as we expected but when we do purple
pink it actually
appends the array that we send
see this is what we're sending it we're
saying an array so it actually makes
that array
an item so if we looked at it if we
wanted to
work with it within this big array
they're indexed by zero so this would be
zero one zero one two
three four five six and so if we looked
at that
and did print
colors can i get it right there we go
color
6.
we can see that it actually it's not
pulling purple it's not appending
each item it's appending that actual
array so if we did that in reverse
if we did the purple pink first
and then the yellow probably more
visible as we see here
that the array gets appended and notice
with that we've got those brackets
then a comma and then yellow so this
would be whatever that was then got
zero one two three four that's item five
and the array is this item six is yellow
so that's a pin we actually can take an
item and put it
dynamically add it to the end of array
next we're going to look at
is copy so let's move this down a little
bit
and i have more example code that i'm
sort of adjusting this time because this
is a second attempt
i had some some technical issues
recording this first time so we're doing
a clear
a clean copy of this so speaking of copy
the next one you use
is copy so if we take colors and copy it
and do a dot copy we're gonna take a
look at what that returns
and then um oops sorry that one i gotta
print it first
so we're gonna do the return value and
then we'll just
for grins print out the
original value
um let me do this
let me do this
[Music]
so we can open that up a little bit more
there we go oh
let's sprint let's do it this way
pardon me okay so let's clear this first
so we can see it up at the top okay
so copy we come in and
we take a look colors two is the result
value when we do copy
that dot copy so here's colors too and
notice that when we turn around and
print colors
it's exactly the same however now they
are
different so if i went to
and let's do colors to append after i do
that
now we'll see that colors 2 got the
yellow appended but colors did not so
those
these two are completely new distinct
lists it's not just uh you know another
pointer into it this is actually a true
deep copy of one array to another
and it's going to be important as we get
into the
the certification test for some of these
is looking at what is
actually returned as well with some of
these things
so copy returns the copy it does not you
know somehow copy it within the existing
copy
so let's move on to
clear the same thing now this time we
had
colors too so we've gone through we've
copied it now we're going to clear it
and we're going to see what colors 2 and
then colors 1 looks like
so here we do a clear onto and it gives
us an empty array
and notice that the original colors
still exist
so we have effectively you know we've
well not affected we have turned our
code or two into an empty list
and that's all clear to us it just gets
rid of all the items no matter how
they're set up and even note that
although we have an array in here
it got just every single item didn't
matter what it was it cleared
it out
next i want to do is we're going to
count
and so let's um
so we're going to do a couple things
here so let's look at each of these
arrays so we're first going to do let's
do it like this print
colors
colors 2.
and array
so now if i take a look those will clear
it first
so if we go for the count
when we count colors
here and so we count red
there is one red in that whole
uh list if we count r it's counting
actual ad it's not counting like within
there so if we count like
r even though there's an r here and r
here
uh you know red and green and i guess
even purple's got one
there's not one that there's not an item
that matches r so we get zero back
um important to note that we have the
empty result set if we try to count red
within that
it's not there so it's an empty set and
it's it's valid you can try to count it
but it's just going to give back zero
no matter what similarly we do the day
array
day array is you know 11 12. so even
though this is
numbers now when i try to count ones
it's not counting
it doesn't count the ones that are in
each of those numbers it's looking for
the item
one so if i did a day
array dot append
one then do the count
oh sorry
let's do this
[Music]
uh let's see day array if we append a
one
let's see how that looks yeah
so here oh interesting
oh i'm sorry mistake there
here i'm counting the string one so i
get zero back
if you see down here but if i count the
number one
then i get a one back so you do have to
unders you have to have
uh type awareness basically as you as
you go through this
so that's your counts it's counting how
often something occurs within the array
the next one we can do is list index
and let's print colors just to be safe
and so here when we go to this one we're
going to count
we're going to try to get the index of
blue then
it's 0 1 2 2 is our answer
uh let's do
uh what was that other one that was
called example
and we're going to do something that
recurs twice so let's do red
with an example
let's take a look at that one
so here if we do example and we do the
index
of red which we know occurs twice
whoops
is it ends up getting us a 0 because it
doesn't know
which one to do if i do green which does
turn out
which there's only one
then green is going to give me the one
so i'm not going to get the first which
you may
expect for list index if i have multiple
occurrences
it's just going to give me back let me
go back to this
and save it's going to give me back zero
not undefined but zero because it's
basically saying
i found more than one so the
index function is essentially finding
one and only one occurrence
otherwise you're going to get a zero
so let's go
to remove
let's put a little line feed in there
first
and so we get here and we're going to
remove
blue so we see here
here's our blue and then it just removes
it and shifts everything over
same kind of thing if i do let's do
example
[Music]
and try to remove red let's see what
that does
example
and so now if we try to do a remove of
red note that it didn't do us any good
is um well i'm sorry it removed the
first one it hit
so in the remove situation
is it removed red but not all of reds so
if i do remove
twice
then let's see where now i don't have
any reds
left in it so it's just going to remove
the first occurrence
and again that's different from for
example like index does not find the
current
the first but remove removes the first
one
and we've already
uh oh i guess this here
[Music]
before i get into this next one we will
take a look real quick at what happens
when you do
the index of white after removing blue
and then we're going to look at doing an
insert so we're going to come back and
we're going to we removed blue up here
and we're going to color on colors we're
going to insert blue back in so let's
see how that looks
[Music]
uh let me clear this get back to the top
there we go
so if we come down here so remove note
that when we removed
blue it shifted white over so now white
gets the index
of two now coming back here we went back
and inserted blue
into index 2 which is 0
1 2 and we've basically rebuilt
our situation there so we shifted
everything but when we insert it back in
it inserts it and shifts everything to
the right
remove shifts to left remove insert
shifts to the right
and then the last one i cover in this
one is
pop
and in this case
we're just going to go in on colors and
we're going to pop and then we're going
to pop again so let's actually well we
just pretty close this will be easy to
see
so if we go to the bottom here we do pop
we see this was the last thing we had
when we do pop pop returns the value the
last value in the
array so this is what pop returns
and then it actually changed you'll see
here colors.pop first we print that and
then we just print the colors array
again
and now it's missing that last item
we call pop again it takes yellow out it
returns yellow
and now we've modified the array without
yellow existing in there anymore so
essentially pop is the reverse of
append when we go to a pen we put stuff
at the end
pop we so sometimes you would expect it
would be push and pop but
it's append and pop rr2 functions
related to that
now do it for this time i think we've
gone long enough in this episode
so we will come back and we will hit
part two of our
list functions as always you can find
this out in our github repository
there'll be links out in the show notes
and we'll catch you next time so 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

well hello and welcome back

27.68

we are continuing our series of python

30.64

certification

32

examples as we walk through the

35.04

python certification series for the

38.48

certified associate in python

40

programming walking through a lot of

41.92

these

42.399

actually we're going to eventually walk

43.28

through all of these items to some

44.64

extent go through some examples

46.8

this episode we are going to look at

49.44

list functions

50.879

now we've worked with lists before and

53.199

this is going to be

54.079

part one because there's a lot to go

57.44

over

58.48

so this time we're just going to take

59.84

our first stab at it and then next

61.6

episode we'll come through and get the

63.359

the second part get the rest of those

65.519

those functions

66.88

we've created some lists before uh i'm

70

going to probably stick with the

72.32

colors and we've also added an example

75.68

list

76.96

and really what we're doing here because

78.799

of some of the examples is we're going

80.24

to see where

81.04

our initial one was red green blue and

82.479

white black the second one

84.72

actually has got blue white and black

88.24

so we have those last three duplicated

90.799

so we can actually see duplicates

92.799

it's not a bunch of unique values it

94.4

actually has more than

96.079

one instance of some values which is

98.159

going to become important

99.84

for some of the makes it useful for some

101.84

of the functions we're going to look at

104.479

the first one we'll look at is append

108.32

and now this one it's pretty

110

straightforward so you take we're going

111.2

to work off this

112.64

colors array and we're just going to go

114.72

in and we're going to say colors append

116.56

and then we're going to give it the

117.6

string

118.32

yellow so let's see how that works out

125.28

so if we do it we see where our initial

127.119

list started out was red green blue

128.72

white black

129.44

we append yellow and then yellow as you

132.08

would expect

132.959

gets appended now an interesting thing

136.319

we've looked before i think there's a

138.72

point in the past where we did an

140.239

example where we had

143.04

we tried to append an array

146.56

and so if we try to add purple and pink

150.959

so let's do that so we got a yellow and

152.8

we're going to turn right around and add

153.84

purple and pink

155.12

what we see now is yellow

158.239

is as we expected but when we do purple

160.56

pink it actually

162.239

appends the array that we send

165.28

see this is what we're sending it we're

166.64

saying an array so it actually makes

168.64

that array

170.239

an item so if we looked at it if we

172.879

wanted to

173.76

work with it within this big array

177.12

they're indexed by zero so this would be

178.8

zero one zero one two

180.8

three four five six and so if we looked

184.239

at that

184.879

and did print

191

colors can i get it right there we go

194.4

color

196.84

6.

198.08

we can see that it actually it's not

199.68

pulling purple it's not appending

201.84

each item it's appending that actual

203.599

array so if we did that in reverse

208.72

if we did the purple pink first

212

and then the yellow probably more

215.28

visible as we see here

217.28

that the array gets appended and notice

219.2

with that we've got those brackets

221.36

then a comma and then yellow so this

223.28

would be whatever that was then got

225.12

zero one two three four that's item five

228.4

and the array is this item six is yellow

235.84

so that's a pin we actually can take an

237.599

item and put it

238.959

dynamically add it to the end of array

241.36

next we're going to look at

242.319

is copy so let's move this down a little

245.76

bit

248.08

and i have more example code that i'm

250.319

sort of adjusting this time because this

251.76

is a second attempt

252.959

i had some some technical issues

254.799

recording this first time so we're doing

256.479

a clear

257.919

a clean copy of this so speaking of copy

261.28

the next one you use

262.16

is copy so if we take colors and copy it

266.24

and do a dot copy we're gonna take a

268.4

look at what that returns

271.12

and then um oops sorry that one i gotta

274.16

print it first

276.8

so we're gonna do the return value and

278.96

then we'll just

279.919

for grins print out the

283.759

original value

288.16

um let me do this

292.24

let me do this

296.55

[Music]

297.919

so we can open that up a little bit more

301.44

there we go oh

306.16

let's sprint let's do it this way

309.44

pardon me okay so let's clear this first

313.52

so we can see it up at the top okay

316.88

so copy we come in and

319.919

we take a look colors two is the result

322.96

value when we do copy

324.32

that dot copy so here's colors too and

327.28

notice that when we turn around and

328.479

print colors

329.84

it's exactly the same however now they

333.28

are

333.68

different so if i went to

339.199

and let's do colors to append after i do

342.08

that

345.44

now we'll see that colors 2 got the

347.6

yellow appended but colors did not so

350

those

350.88

these two are completely new distinct

354.08

lists it's not just uh you know another

356.96

pointer into it this is actually a true

359.039

deep copy of one array to another

363.039

and it's going to be important as we get

364.639

into the

366.72

the certification test for some of these

369.12

is looking at what is

370.24

actually returned as well with some of

373.28

these things

374.24

so copy returns the copy it does not you

377.039

know somehow copy it within the existing

378.88

copy

382

so let's move on to

387.28

clear the same thing now this time we

390.24

had

390.8

colors too so we've gone through we've

392.24

copied it now we're going to clear it

394

and we're going to see what colors 2 and

395.6

then colors 1 looks like

398.479

so here we do a clear onto and it gives

401.759

us an empty array

403.199

and notice that the original colors

404.96

still exist

406.319

so we have effectively you know we've

408.8

well not affected we have turned our

411.52

code or two into an empty list

414.56

and that's all clear to us it just gets

416

rid of all the items no matter how

417.44

they're set up and even note that

418.96

although we have an array in here

420.88

it got just every single item didn't

422.479

matter what it was it cleared

424.08

it out

429.84

next i want to do is we're going to

434.84

count

438.16

and so let's um

449.52

so we're going to do a couple things

450.639

here so let's look at each of these

452.639

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

455.199

do it like this print

457.68

colors

461.36

colors 2.

465.039

and array

475.44

so now if i take a look those will clear

477.36

it first

478.56

so if we go for the count

482

when we count colors

485.12

here and so we count red

488.879

there is one red in that whole

492.4

uh list if we count r it's counting

495.599

actual ad it's not counting like within

497.28

there so if we count like

498.319

r even though there's an r here and r

500.16

here

501.52

uh you know red and green and i guess

502.879

even purple's got one

505.12

there's not one that there's not an item

506.639

that matches r so we get zero back

511.599

um important to note that we have the

513.76

empty result set if we try to count red

516.159

within that

517.039

it's not there so it's an empty set and

519.12

it's it's valid you can try to count it

521.2

but it's just going to give back zero

522.56

no matter what similarly we do the day

525.36

array

527.44

day array is you know 11 12. so even

530

though this is

530.56

numbers now when i try to count ones

533.04

it's not counting

534.32

it doesn't count the ones that are in

535.6

each of those numbers it's looking for

537.36

the item

538.399

one so if i did a day

541.6

array dot append

546.839

one then do the count

549.68

oh sorry

555.36

let's do this

561.92

[Music]

567.04

uh let's see day array if we append a

569.519

one

570.16

let's see how that looks yeah

573.2

so here oh interesting

578.72

oh i'm sorry mistake there

581.92

here i'm counting the string one so i

583.76

get zero back

584.959

if you see down here but if i count the

587.519

number one

590.88

then i get a one back so you do have to

592.88

unders you have to have

595.2

uh type awareness basically as you as

597.839

you go through this

600.32

so that's your counts it's counting how

603.279

often something occurs within the array

605.839

the next one we can do is list index

611.92

and let's print colors just to be safe

620.399

and so here when we go to this one we're

622.8

going to count

623.68

we're going to try to get the index of

624.959

blue then

628

it's 0 1 2 2 is our answer

632

uh let's do

635.36

uh what was that other one that was

636.8

called example

638.959

and we're going to do something that

640

recurs twice so let's do red

642.72

with an example

646.959

let's take a look at that one

650.8

so here if we do example and we do the

653.2

index

654.72

of red which we know occurs twice

662

whoops

665.36

is it ends up getting us a 0 because it

667.68

doesn't know

668.48

which one to do if i do green which does

671.839

turn out

675.839

which there's only one

678.959

then green is going to give me the one

680.399

so i'm not going to get the first which

682.079

you may

682.48

expect for list index if i have multiple

685.2

occurrences

685.839

it's just going to give me back let me

688

go back to this

690.88

and save it's going to give me back zero

692.959

not undefined but zero because it's

694.48

basically saying

695.44

i found more than one so the

699.12

index function is essentially finding

701.519

one and only one occurrence

703.279

otherwise you're going to get a zero

716.32

so let's go

719.44

to remove

722.959

let's put a little line feed in there

724.88

first

726.88

and so we get here and we're going to

730.32

remove

730.88

blue so we see here

734.24

here's our blue and then it just removes

737.04

it and shifts everything over

739.2

same kind of thing if i do let's do

742.839

example

746.39

[Music]

748.56

and try to remove red let's see what

750.639

that does

756.839

example

761.839

and so now if we try to do a remove of

763.6

red note that it didn't do us any good

767.36

is um well i'm sorry it removed the

771.04

first one it hit

772.48

so in the remove situation

776

is it removed red but not all of reds so

779.6

if i do remove

780.56

twice

784.839

then let's see where now i don't have

787.12

any reds

788.079

left in it so it's just going to remove

789.92

the first occurrence

793.36

and again that's different from for

794.959

example like index does not find the

796.56

current

797.839

the first but remove removes the first

800.16

one

802.48

and we've already

806

uh oh i guess this here

809.03

[Music]

811.12

before i get into this next one we will

813.36

take a look real quick at what happens

814.72

when you do

816.16

the index of white after removing blue

822

and then we're going to look at doing an

823.36

insert so we're going to come back and

824.72

we're going to we removed blue up here

827.519

and we're going to color on colors we're

829.68

going to insert blue back in so let's

831.36

see how that looks

832.23

[Music]

833.36

uh let me clear this get back to the top

836.8

there we go

840.079

so if we come down here so remove note

842.959

that when we removed

845.519

blue it shifted white over so now white

848.959

gets the index

850

of two now coming back here we went back

852.959

and inserted blue

854.48

into index 2 which is 0

857.92

1 2 and we've basically rebuilt

861.839

our situation there so we shifted

864.079

everything but when we insert it back in

866

it inserts it and shifts everything to

868.639

the right

869.519

remove shifts to left remove insert

872.56

shifts to the right

877.68

and then the last one i cover in this

879.839

one is

882.839

pop

885.279

and in this case

889.68

we're just going to go in on colors and

891.199

we're going to pop and then we're going

892.079

to pop again so let's actually well we

893.68

just pretty close this will be easy to

895.12

see

896.24

so if we go to the bottom here we do pop

898.16

we see this was the last thing we had

902.079

when we do pop pop returns the value the

905.76

last value in the

907.04

array so this is what pop returns

910.079

and then it actually changed you'll see

913.12

here colors.pop first we print that and

915.199

then we just print the colors array

916.48

again

917.36

and now it's missing that last item

920.48

we call pop again it takes yellow out it

923.04

returns yellow

924.48

and now we've modified the array without

927.519

yellow existing in there anymore so

930.959

essentially pop is the reverse of

934.079

append when we go to a pen we put stuff

936.079

at the end

937.12

pop we so sometimes you would expect it

939.12

would be push and pop but

940.399

it's append and pop rr2 functions

943.759

related to that

945.92

now do it for this time i think we've

947.12

gone long enough in this episode

949.279

so we will come back and we will hit

950.8

part two of our

952.48

list functions as always you can find

954.24

this out in our github repository

956.399

there'll be links out in the show notes

958.639

and we'll catch you next time so go out

960.639

there have yourself a great day

962.24

a great week and we will talk to you

964.88

next time

982.399

you