- [Instructor] In this video we'll continue
the iPython session from the preceding video
and we'll keep working with our list called numbers
this time to modify the contents of the list
using slicing operations.
So let me paste in our initial slice operation
that we're going to discuss here.
We have the list numbers, and we're saying
select the subset of that list with the slice
of that list from index zero,
up to but not including index three.
Now just at the top here, here's the original list
that we're talking about.
So from index zero we're going to go zero, one, two,
up to but not including index position three.
So we're talking about the slice containing two,
three, and five, and we're going to assign to that slice
this list with the same number of elements.
In this case two, three, and five as the words
that we would like to replace the numeric values.
And by the way, when you're assigning to a slice,
you do not need to
have the same number of elements
in the new slice as in the original slice,
so you can actually change the size of a slice
by assigning a sequence of values to that slice,
and we'll talk about that a couple of times in this video.
So let's execute that and of course to check what's
now in the list numbers we can evaluate it,
and you can see that we've replaced the first
three elements with the word equivalents of those values.
Now in addition to simply replacing values within a slice
we can delete elements using slice notation as well.
So, for example, if I say number sub-zero colon three again
we're still selecting the first three elements of that list.
And if we assign to that an empty list,
that literally says delete that slice
from the original list.
So in this case to prove that we can go ahead
and say numbers, and now you see the first
three elements have been physically removed from the list.
Now
so far, we've demonstrated
a couple of modification operations.
Let's bring back the original list for the next operation
that we're going to present to you.
And by the way, here's a handy little trick
that you can use to recall a prior snippet.
So I know at some point previously,
I assigned something to the variable numbers.
If I type in the beginning of that assignment
and hit the up arrow key it will recall the last statement
that started with what I had typed so far.
So I'm now going to reset the variable numbers
to a list containing the same values
that we started with in the preceding video.
Now,
we can
assign
slices to the list
in non-consecutive elements,
and so that's what I want to demonstrate next here.
So if I say numbers, colon, colon, two,
that says for every other element
starting from the first element of the list numbers,
we would like to select that slice.
And let's say we wanted to assign the value 100
to every other element.
So we have eight elements in this case, we want to
place 100 into element zero,
two, four, and six.
So as a result of this it's going to take
the first value in the right-hand side and
place it into the first element of the resulting slice,
the second value on the right-hand side will be placed
into the second element of the resulting slice,
and we'll do the same thing for the third
and fourth elements of the resulting slice as well.
And of course if we now say numbers,
you can see the four 100 values placed
into the corresponding locations within that list.
Now for the next part of this example,
the first thing I want to do is just take the ID of numbers,
because I want to show you when new objects are being
created versus modifying the existing object.
So we'll simply leave this here for the next
part of the discussion.
So we showed you up above that you can
assign an empty list to a slice
to delete elements from that list.
Well, it turns out that you can use that technique
to delete all of the elements of the list as well.
So for example, if I say numbers, sub-colon,
like that,
it's going to implicitly insert the zero for
the starting index, and the length of numbers for
the ending index.
And I can then assign to that an empty list
which literally deletes all the elements
from the original list.
So if I go ahead and execute that operation,
I can evaluate numbers to see that indeed it's empty.
And I just want to prove to you that it's the same
list object still.
So let me recall my ID expression here,
and when I execute that you can see
that the ID's of these two objects are identical,
so it's still the same numbers list,
we've simply deleted all the elements by using
a slice operation on the left side of the assignment.
Now that's very different from doing this assignment
which simply aims the variable numbers
at a completely new list object that contains no elements.
So let's go ahead and execute that,
and of course if we evaluate numbers
we still see that it's empty,
but if we recall that ID expression once again,
you can see this time that indeed
we have a new object identity,
so this was a physically new object,
as opposed to modifying the existing one.