1 00:00:00,180 --> 00:00:07,700 In this tutorial we are going to discuss about two concepts aliasing lists and cloning. 2 00:00:07,740 --> 00:00:20,180 Lis will start with aliasing aliasing happens when one variables value is assigned to another variable. 3 00:00:20,470 --> 00:00:22,390 So consider this example. 4 00:00:22,390 --> 00:00:33,160 We have defined a variable E which is assigned a string Python within defined another variable B and 5 00:00:33,170 --> 00:00:38,450 using the assignment operator we have assigned a to b. 6 00:00:39,260 --> 00:00:44,900 So that means B also contains the string python. 7 00:00:45,560 --> 00:00:55,760 So in the memory the value Python does being referenced by two variables A and B. 8 00:00:55,770 --> 00:01:07,430 Now I change the value of debatable a so I'm now going to assign another string to the variable. 9 00:01:07,840 --> 00:01:20,800 So that means A is now pointing to the string Python three dot six and variable B is now pointing to 10 00:01:21,800 --> 00:01:22,210 bite. 11 00:01:22,240 --> 00:01:29,770 And when we change do while you off one of the variables the value that is already present in this memory 12 00:01:29,770 --> 00:01:37,320 location is not changed but but the variable A is now pointing to another value. 13 00:01:37,330 --> 00:01:41,010 Another string which is in a different memory location. 14 00:01:41,020 --> 00:01:42,500 Now consider the case. 15 00:01:42,580 --> 00:01:50,140 So I have defined a list D1 which contains three elements One two and three. 16 00:01:50,260 --> 00:01:59,540 And then I have defined another list a list to do and using the assignment operator I have assigned 17 00:01:59,600 --> 00:02:01,990 a list D1 to industry 2. 18 00:02:02,450 --> 00:02:10,300 So that means a list D1 and a list to have the same set of elements 1 2 and 3. 19 00:02:10,430 --> 00:02:18,650 That means a list you 1 and a list you do are pointing to the same memory address containing these three 20 00:02:18,650 --> 00:02:19,950 elements. 21 00:02:20,000 --> 00:02:32,380 So if your check in the Jupiter notebook to verify if both LSD 1 and LSD 2 are pointing to the same 22 00:02:32,380 --> 00:02:36,950 memory address the result would be true. 23 00:02:37,030 --> 00:02:41,190 Let us now add a new element to the list. 24 00:02:41,200 --> 00:02:45,020 LSD 1 using say the append method. 25 00:02:45,370 --> 00:02:53,740 So I'm going to say LSD 1 bought the pan and I'm going to pass the argument. 26 00:02:53,770 --> 00:03:04,730 As for that would change a list to 1 2 1 2 3 and 4. 27 00:03:05,260 --> 00:03:08,170 So what would happen to the list. 28 00:03:08,320 --> 00:03:12,960 LSD to let us check that in Jupiter notebook. 29 00:03:13,120 --> 00:03:22,390 So as we have discussed I have assigned the list containing the element 1 2 and 3 to list 1 and then 30 00:03:22,450 --> 00:03:28,680 I have assigned list 1 to list 2 using the assignment operator. 31 00:03:28,780 --> 00:03:37,960 So list 2 also contains the same set of elements and then I have verified if list 1 and list 2 at binding 32 00:03:37,960 --> 00:03:42,500 to the same memory address and the result is true. 33 00:03:43,520 --> 00:03:59,800 Next I have now appended the integer 4 to list 1 and 1 to display the list 1 at least 2 1 the value 34 00:03:59,800 --> 00:04:10,980 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 35 00:04:10,980 --> 00:04:21,590 elements in list to so list two has also been changed so in the memory location 36 00:04:24,940 --> 00:04:27,580 the list has been changed in place. 37 00:04:27,700 --> 00:04:37,590 That means Indy memory location devalue Ford has been appended to the list so all of the objects that 38 00:04:37,590 --> 00:04:42,580 are pointing to this memory address have been affected. 39 00:04:43,300 --> 00:04:52,780 So this is a side effect of aliasing lists and although this behavior can be useful it does sometimes 40 00:04:52,870 --> 00:04:55,810 unexpected or undesirable. 41 00:04:56,440 --> 00:05:04,290 And in general it is safer to wide aliasing when we are working with mutable objects such as list. 42 00:05:04,900 --> 00:05:15,250 So if the data is immutable say strings aliasing does not matter because the data cannot change if the 43 00:05:15,250 --> 00:05:20,290 data is changed the variable points to a different memory location. 44 00:05:20,440 --> 00:05:30,860 As we have seen with strings in our examples in case of lists because lists are mutable aliasing can 45 00:05:30,860 --> 00:05:34,090 cause unexpected results. 46 00:05:34,130 --> 00:05:42,230 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 47 00:05:42,230 --> 00:05:46,780 the list itself and not just the reference. 48 00:05:46,820 --> 00:05:57,390 And this process is called cloning sometimes we may not want to make a difference because a copy works 49 00:05:57,600 --> 00:05:58,050 better. 50 00:05:59,270 --> 00:06:03,680 So let us see how lists can be cloned. 51 00:06:03,800 --> 00:06:06,430 So here we have a list. 52 00:06:06,470 --> 00:06:15,740 LSD one containing three elements 1 2 and 3 and if we slice this list from the beginning to the end 53 00:06:15,860 --> 00:06:19,580 of the list we make a new copy of the list. 54 00:06:20,710 --> 00:06:25,780 So let us create a new list. 55 00:06:25,810 --> 00:06:40,700 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 56 00:06:42,520 --> 00:06:47,090 and only give the colon that means the list. 57 00:06:47,530 --> 00:06:52,570 LSD 1 will be sliced from beginning to the end of the list. 58 00:06:52,690 --> 00:06:59,960 So list 3 would now have the element 1 2 and 3. 59 00:07:00,070 --> 00:07:09,400 So we have 2 lists LSD one and a list you do and they are pointing to 2 different memory addresses containing 60 00:07:09,400 --> 00:07:11,860 the same set of elements. 61 00:07:11,860 --> 00:07:15,090 So let us now see this in the Jupiter notebook. 62 00:07:17,170 --> 00:07:19,320 So here I have the list. 63 00:07:19,330 --> 00:07:20,690 LSD 1. 64 00:07:21,010 --> 00:07:24,860 And it does assign the element 1 2 and 3. 65 00:07:25,240 --> 00:07:35,300 Now in order to create a copy of LSD 1 I can use the slicing operation on list 1. 66 00:07:35,770 --> 00:07:39,130 So slicing the list. 67 00:07:39,130 --> 00:07:47,770 LSD one from the beginning to the end of the list would create a copy of LSD 1 and this copy is called 68 00:07:47,860 --> 00:07:48,620 LSD 3. 69 00:07:48,670 --> 00:07:51,800 So I've displayed this list LSD 3. 70 00:07:51,910 --> 00:08:00,460 So let us now see if the list LSD 1 and a listed 3 are pointing to the same memory location or not using 71 00:08:00,460 --> 00:08:04,870 the identity operator IE is is false. 72 00:08:04,880 --> 00:08:08,000 They're not pointing to the same memory address. 73 00:08:08,000 --> 00:08:16,350 Now let us go and change list 3 so I'm gonna change the first element in list. 74 00:08:16,350 --> 00:08:17,580 LSD 3. 75 00:08:17,780 --> 00:08:21,370 I want to sell the index. 76 00:08:21,380 --> 00:08:23,380 0 should be 5. 77 00:08:23,570 --> 00:08:26,010 No letters display listed 3. 78 00:08:26,240 --> 00:08:36,320 As you can see the value at index 0 has been changed after we have assigned the 0 8 index in listed 79 00:08:36,320 --> 00:08:38,080 3 to 5. 80 00:08:38,100 --> 00:08:44,390 Now what about a listing 1 Let's display this. 81 00:08:44,560 --> 00:08:51,940 It is still pointing to the memory address having the integers 1 2 and 3. 82 00:08:52,290 --> 00:09:03,160 Now letters and another number to a list G3 using the append method executed this list display enlisted 83 00:09:03,200 --> 00:09:03,940 3. 84 00:09:03,970 --> 00:09:08,110 So it is 5 2 3 and 9 are the elements. 85 00:09:08,150 --> 00:09:10,260 How about LSD 1. 86 00:09:10,280 --> 00:09:17,670 So it is still pointing to the old memory address containing these three elements. 87 00:09:17,870 --> 00:09:26,270 So we are free to make changes to the new list without worrying that we would be unknowingly change 88 00:09:26,330 --> 00:09:27,940 the original list. 89 00:09:27,980 --> 00:09:30,760 And this is possible by cloning. 90 00:09:30,830 --> 00:09:31,220 Less.