Welcome back.
Earlier in this specialization, we took dive into aliasing, and the confusions
that can occur when you mutate a list or a dictionary that has many aliases.
Many different variable names pointing to the same list or dictionary.
The same thing is true if you have instead of variable names
pointing to a single list,
if you have a single list that is included as an element in multiple other lists.
If you mutate the inner list that is part of several outer lists,
all those outer lists will look like the contents have changed.
The best to keep track of these confusing situations will be
with the reference diagram, of course.
So let's took at example one way that you get a list included in multiple list
by accident, and let's get this confusing situation is when you copy a list.
As in this codes slip it, let's start by just running it.
So you can see that on line1, we create a list called original,
it's value is a nested list.
The outer list has two inner lists as it's elements.
Let's set ourselves up with a reference diagram.
We're going to say that, Original,
Is the variable name, and it's value is a list.
That list has two items.
The first item is itself a list, and that list has dogs,
As an element, and it has puppies as an element.
The second element of original is another list,
Which is as it values cats and kittens.
So that's how it happens on line 1.
On line 2, we make a copy of original.
We use the slice operator, colon inside of square brackets.
You may recall that always creates a slice.
If I said, original[1:4],
that would Make a slice beginning
with the item at index one and going up to but not including position four.
Of course, I can't do that for this list because it only has two items.
But if we leave off the value before the colon,
we start at the beginning of the list.
If we leave off the value after the colon we go all the way to the end of the list.
So this says take a slice beginning at the beginning of original, and
going to end of original.
So that makes a new list, and that new list has has its elements,
the same elements that original had.
The first element is dogs and puppies, the second element is cats and kittens.
And we assign that to a variable called copied_version.
That's what happens on line 2.
And just to show you what the output looks like.
On line 3, we print out copied _version and sure enough,
it looks like it has exactly the same stuff that original has in it.
On line 4, we checked is copied_version the same object as original.
And the answer to that is false.
Copied_version is pointing to one object.
Original is pointing to a different object.
One line 5, we check whether they have the same contents.
The answer to that true.
So original and copied are not the same object,
they're different objects, but they're both list.
They both have two elements, and those elements are these inner list,
dog, puppies, and cats, kittens.
It's true that they are equal, equal.
It's not true that they are is each other.
So copied_version is not original but
it is equal to the original, it has the same contents.
On line 6, we see where our confusions are going to happen
when we have multiple outer list that point to the same inner list.
So original square bracket 0 is the first inner list, that's dog's puppies.
And on line 6, we say, .append of canines.
So that is going to append to the end of this list, another item.
But notice that this item is canines in square brackets.
So this item is actually another list.
That list has only one item, canines.
But it is a list, it's not just this string canines.
So now on line 7, if we print original, We get this list here.
Where the first element is dogs, puppies like before, but
we've appended to the end of that list, the list canines.
And then the second element is still cats and kittens.
On line 9, we print the copied version.
Now on line 6, we just said to change original,
we didn't say anything about changing copied_version.
But on line 9, you can see
that this square bracket canines has magically appeared after dogs and puppies.
And that's because copied_version, even though we didn't say to change it,
copied_version has as its first element this list, and we mutated that list.
That's shallow copies for you.
See you next time.