In this tutorial we are going to discuss about two concepts aliasing lists and cloning.
Lis will start with aliasing aliasing happens when one variables value is assigned to another variable.
So consider this example.
We have defined a variable E which is assigned a string Python within defined another variable B and
using the assignment operator we have assigned a to b.
So that means B also contains the string python.
So in the memory the value Python does being referenced by two variables A and B.
Now I change the value of debatable a so I'm now going to assign another string to the variable.
So that means A is now pointing to the string Python three dot six and variable B is now pointing to
bite.
And when we change do while you off one of the variables the value that is already present in this memory
location is not changed but but the variable A is now pointing to another value.
Another string which is in a different memory location.
Now consider the case.
So I have defined a list D1 which contains three elements One two and three.
And then I have defined another list a list to do and using the assignment operator I have assigned
a list D1 to industry 2.
So that means a list D1 and a list to have the same set of elements 1 2 and 3.
That means a list you 1 and a list you do are pointing to the same memory address containing these three
elements.
So if your check in the Jupiter notebook to verify if both LSD 1 and LSD 2 are pointing to the same
memory address the result would be true.
Let us now add a new element to the list.
LSD 1 using say the append method.
So I'm going to say LSD 1 bought the pan and I'm going to pass the argument.
As for that would change a list to 1 2 1 2 3 and 4.
So what would happen to the list.
LSD to let us check that in Jupiter notebook.
So as we have discussed I have assigned the list containing the element 1 2 and 3 to list 1 and then
I have assigned list 1 to list 2 using the assignment operator.
So list 2 also contains the same set of elements and then I have verified if list 1 and list 2 at binding
to the same memory address and the result is true.
Next I have now appended the integer 4 to list 1 and 1 to display the list 1 at least 2 1 the value
forward has been appended to the end of the list and this is the list 1 2 3 and 4 No let us check the
elements in list to so list two has also been changed so in the memory location
the list has been changed in place.
That means Indy memory location devalue Ford has been appended to the list so all of the objects that
are pointing to this memory address have been affected.
So this is a side effect of aliasing lists and although this behavior can be useful it does sometimes
unexpected or undesirable.
And in general it is safer to wide aliasing when we are working with mutable objects such as list.
So if the data is immutable say strings aliasing does not matter because the data cannot change if the
data is changed the variable points to a different memory location.
As we have seen with strings in our examples in case of lists because lists are mutable aliasing can
cause unexpected results.
If we want to modify a list and also keep a copy of the original we need to be able to make a copy of
the list itself and not just the reference.
And this process is called cloning sometimes we may not want to make a difference because a copy works
better.
So let us see how lists can be cloned.
So here we have a list.
LSD one containing three elements 1 2 and 3 and if we slice this list from the beginning to the end
of the list we make a new copy of the list.
So let us create a new list.
LSD 3 and this LSD 3 is a copy of LSD 1 and if we do not specify the first and the second index numbers
and only give the colon that means the list.
LSD 1 will be sliced from beginning to the end of the list.
So list 3 would now have the element 1 2 and 3.
So we have 2 lists LSD one and a list you do and they are pointing to 2 different memory addresses containing
the same set of elements.
So let us now see this in the Jupiter notebook.
So here I have the list.
LSD 1.
And it does assign the element 1 2 and 3.
Now in order to create a copy of LSD 1 I can use the slicing operation on list 1.
So slicing the list.
LSD one from the beginning to the end of the list would create a copy of LSD 1 and this copy is called
LSD 3.
So I've displayed this list LSD 3.
So let us now see if the list LSD 1 and a listed 3 are pointing to the same memory location or not using
the identity operator IE is is false.
They're not pointing to the same memory address.
Now let us go and change list 3 so I'm gonna change the first element in list.
LSD 3.
I want to sell the index.
0 should be 5.
No letters display listed 3.
As you can see the value at index 0 has been changed after we have assigned the 0 8 index in listed
3 to 5.
Now what about a listing 1 Let's display this.
It is still pointing to the memory address having the integers 1 2 and 3.
Now letters and another number to a list G3 using the append method executed this list display enlisted
3.
So it is 5 2 3 and 9 are the elements.
How about LSD 1.
So it is still pointing to the old memory address containing these three elements.
So we are free to make changes to the new list without worrying that we would be unknowingly change
the original list.
And this is possible by cloning.
Less.