- [Man] In this video we're going to take a look
at a variety of other list methods that you have available
to you and a couple of times now, I've shown you
that when you type a list name and then a dot and a Tab,
Ipython will show you the complete list of methods that
you're allowed to call on that list object.
So for this example or series of examples, rather,
let's start with a list called color names
and let's also begin by demonstrating various ways
to add elements to a list.
So to begin,
we're going to use
the insert method, which as its name implies,
is going to insert an item
into the list and the way it works is you specify
the index number where you'd like to place a value
and let's say we want to insert red in this list.
The index number is the point at which the new value
will be placed; whatever is already there will be,
if you will, shifted to the right in the collection
and will now have a index one higher than that.
So at index zero, right now is orange,
it's now going to be be index one in the updated list.
So if I go ahead and display color names after
that operation, we can see index zero is now red
and index one is now orange.
Now, another way to add...
one item to the list is
with the append method and as its name implies,
it's going to add that to the end of the list.
So if I go ahead and say I wanna add the object blue
to the end of the list, I can then reevaluate color names
and now we see that the append operation
did indeed place an item at the end of the list.
Now, both of these methods insert one object into the list.
In the first case wherever you want.
In the second case only at the end.
Separately, you have the ability to add a whole bunch
of items to the end of a list
and there's a couple of ways to that.
One of which, is the extend method.
So actually for this one, let me copy and paste
a snippet of code to execute.
So here what we have is our list color names,
the extend method that we'd like to call and the argument
to this method is a sequence of objects that you w like
to add to the end of the list and what would happen in this
case, is each individual item, within that sequence,
will be placed at the end of the list color names.
So if I go ahead and execute that and then, once again,
reevaluate color names, you can see that both indigo
and violet were added to the end of list.
I did not take the entire list object
and add that as one item into the list.
It actually iterates through the elements,
taking one item at a time from the sequence argument
and adding them to the end.
Now this is actually the equivalent of using
the plus equals operator.
Let's go ahead and look at this
extend method in a little bit more detail.
Let's start with a list that's empty for example
and let's demonstrate what happens
when you extend a list with a string.
So let' say we have a string that's just ABC,
for demonstration purposes here,
and we want to add 'em to the end of the sample list.
So we'll use the extend method once again
and we'll give it the sequence S as an argument.
So we can see what the sample list looks like after
that operation and you see that it picks off
each individual entity within the sequence,
that you pass as an argument, and the same thing
would be true by the way with a tuple.
So if I have a tuple called T and I give it a few values
and then I say to sample list
to extend with T as an argument,
it's going to pick off the one, the two
and the three and add them to the end of the sample list.
So you have one list with three elements that came from
the string and three elements that came from that tuple.
Now, in each of these two cases we took a variable
that represented a sequence
and passed it into the extend method.
What if you just wanted to pass
a tuple directly to the extend method?
So, let's say we want to call sample list
and I'm not going to execute this yet
but let's just type it first.
So, let's say I wanna add four, five and six.
Whoops, I forgot the extend call.
(keyboard clicking)
So, let's say we wanna add four, five and six
to the end of sample list, you might be tempted to write
the expression this way but the extend method requires one
argument only and that argument must be sequence of values.
So if you accidentally pass in more than one element,
which is what I'm doing here, you would actually
get a type error, if you were to execute this code.
So again, it expects one object to be passed as an argument.
So if you want to pass a tuple of values into the extend
method, you would have to actually wrap them
in an additional set of parentheses.
So now what we have here, inside the parentheses,
is actually a tuple containing four, five and six.
We're passing one object to the extend method, a tuple,
it's then iterating through the tuple to evaluate
the elements and we can see that we're able to add
four, five and six to the end of the list.
So, at this point we've shown you that you can
insert and append items to a list.
Now let's talk about removing items from a list.
So for example, if we go ahead and, whoops,
if we say color_names.remove
(keyboard clicking) and let's say we
want to remove the string green,
so we're talking about the color names list up above here
once again, and you can see the string green is in there.
So we have this remove method for that purpose
and it's going to remove the very first item it encounters
that has that value and of course we only have one such item
in this particular case and now if we evaluate color names,
after that remove operation, indeed, green is gone.
Now this operation also will result in a value error,
if the specified argument is not found within the list.
So you want to be aware of that issue.
Now, you can also completely empty out a list.
We demonstrated a way to do that using slicing
in a proceeding video but there's also the clear method.
So I can say color_names.clear and if I then evaluate
color names after that operation, you can see
there's no elements left in the list.
And that would have been the equivalent
of saying color_names[:].
So, just an empty set of square brackets
with a colon in the middle and we could've assigned
to that the empty list and that's the equivalent
of calling clear and this case they happen
to take up the same number of characters,
so there you can really choose either way
and use the one that's your preference.
Okay, so we've looked at a number
of different methods so far.
Next, what we'd like to do is talk about counting up
the number of occurrences of a given item within a list.
So, for this purpose I'm going to paste in a responses
list that has a whole bunch of numeric values
from one through five in it and these might be responses
to a survey, for example, and then what we want
to do is just total up those responses.
So, I'm going to use a little for loop for that purpose
and I'll paste it in and then explain it to you.
So here we have a for loop where the target variable I
is going to be a value from one through five.
Those happen to be the values in my responses list
and for every one of those items we're going
to say the item, whatever I is at this point appears
and then we're going to figure out
how many times it appears in the responses list.
So response.count is the key part of this print statement.
Responses.count with the argument I says count up how many
times the value I appears in that list.
And for each of those items we can see how many times
each of those occur and of course you can go back
and count them up yourself to confirm
that these count values are, in fact, correct.
Now count, by the way, was one of those descriptive
statistics that we introduced in an earlier lesson, as well.
So if you a list full of a large number items
and you were trying to do a little bit of descriptive
statistics on the list, one of the things
you might do is something like this for loop here.
Now a couple more items that we have for lists,
we also have the ability to reverse a lists elements.
So let's start by redefining color names list here
and I'm mainly doing this so that we can see it close
to the snippet of code that I'm about to execute.
So we can see the order of the strings at the moment.
If I go and say color_name.reverse ...
that's going to reserve the items in the list in place.
It's modifying the list, so if I do that and then evaluate
color names you can see, indeed,
they have been reversed inside that list.
So that's a convenient way to quickly reverse
the items in a collection, in a list, specifically.
And then finally there's one other method
that we want to point out in this video.
The method copy, so let's create
a copied list variable for a moment.
If I say color_names.copy, this is going
to perform a shallow copy of the color names list.
What that means is that I will get another list
with the same number of elements as color names
and the elements will refer to the same objects
that color names refers to.
So, blue, green, yellow, orange and red as strings
will exist in memory and both the color names list
and the copied list are going to refer to the same elements.
So if now go and say copied_list, you can see we have
what appears to be a copy of the original list
and remember you can always go and use that ID function
to confirm that we really do have the same object.
So, for example, if go and say ID of color_names[0],
we can see what the ID number of color names is for that
and If I repeat that for the copied list...
I add element zero, we can see we get the exact same
ID value, which is further confirmation that both of these
lists have elements referring to the same objects in memory.