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
[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