1 00:00:00,720 --> 00:00:06,860 You know, a tutorial on Python, we abuse this damn shallow copy and deep copy for multiple number 2 00:00:06,860 --> 00:00:07,340 of times. 3 00:00:07,680 --> 00:00:12,790 So in this video, we are going to discuss what is shallow copy and what is a deep copy. 4 00:00:13,220 --> 00:00:19,730 So let us go for one practical demonstration to show you the differences between this shallow and the 5 00:00:19,730 --> 00:00:20,370 deep copy. 6 00:00:20,690 --> 00:00:23,570 So here is the demonstration in Python. 7 00:00:23,570 --> 00:00:27,920 We use the equal to operator to create a copy of an object. 8 00:00:28,280 --> 00:00:32,660 And we may think that that will create a new object, but it is not. 9 00:00:32,660 --> 00:00:37,790 So it only creates a new variable that shows the difference of the original object. 10 00:00:38,810 --> 00:00:40,400 Let us consider this example here. 11 00:00:40,820 --> 00:00:42,080 So here you have defined one. 12 00:00:42,080 --> 00:00:45,320 Least one, at least two is equal to least one here. 13 00:00:45,320 --> 00:00:47,450 Do you see we have used the equal to operator. 14 00:00:48,050 --> 00:00:49,250 So now what is happening? 15 00:00:49,250 --> 00:00:54,480 You can find that if you print this list, one, if you print this list to getting the output like this. 16 00:00:54,950 --> 00:00:56,450 So here is the respective output. 17 00:00:56,450 --> 00:00:57,920 We are having this output like this. 18 00:00:58,250 --> 00:01:01,880 Also, we are going to print the idea of at least one idea of these two. 19 00:01:02,150 --> 00:01:05,210 You can find that both these two ideas are simple. 20 00:01:05,660 --> 00:01:13,220 So that means here we are actually creating a new variable that will share the difference of the original 21 00:01:13,220 --> 00:01:13,740 object. 22 00:01:13,910 --> 00:01:18,440 So here the new variable is linked to an original object is our last one. 23 00:01:19,100 --> 00:01:22,250 So now let us go for this list, one dot append. 24 00:01:22,280 --> 00:01:25,320 So now here to see we are spending some new elements here. 25 00:01:25,700 --> 00:01:28,430 So now we are going to print this list one and list two. 26 00:01:28,430 --> 00:01:34,430 But we are finding that the new variables, whatever new values, whatever we are going to append is 27 00:01:34,430 --> 00:01:39,650 getting reflected, is getting accessed from what, at least one analyst list to here. 28 00:01:40,130 --> 00:01:44,230 So this is our that is a copy using our equal to operate. 29 00:01:44,780 --> 00:01:47,790 So now we are going to discuss another two kinds of copy. 30 00:01:47,900 --> 00:01:49,780 So there are two ways to create a copy. 31 00:01:49,910 --> 00:01:56,110 One is the shallow copy and the one is a deep copy will be going to discuss on this very aspect now. 32 00:01:56,630 --> 00:02:00,310 And to make this copy work, we use the copy model. 33 00:02:00,320 --> 00:02:02,790 So that's why you have done the import copy here. 34 00:02:03,080 --> 00:02:09,890 So here you are using this copy model and we use the copy model of Python for shallow and the deep copy 35 00:02:09,890 --> 00:02:10,670 operations. 36 00:02:11,090 --> 00:02:16,830 And suppose we need to copy the compound, say if we need to copy a complete list. 37 00:02:16,940 --> 00:02:19,040 So in that case, it will be done in this way. 38 00:02:19,190 --> 00:02:26,120 That is a copy copy or list and that will be assigned to the new list or later I can show you that will 39 00:02:26,120 --> 00:02:31,010 be using copy dot deep copy onliest and that will be assigned to the new list. 40 00:02:31,310 --> 00:02:32,840 So let us discuss one by one. 41 00:02:33,200 --> 00:02:36,010 So at first we are going to discuss what is a shallow copy. 42 00:02:36,500 --> 00:02:42,980 A shallow copy actually creates a new object which stores the reference of the original elements and 43 00:02:42,980 --> 00:02:44,900 so, so is shallow. 44 00:02:44,900 --> 00:02:47,720 Copy does not create a copy of the nested objects. 45 00:02:48,140 --> 00:02:51,860 Instead it just copies the reforms of the nested objects. 46 00:02:52,170 --> 00:02:59,750 That means that a copy process does not recurse or create copies of the NextEd objects itself. 47 00:03:00,230 --> 00:03:01,840 It the things are not getting clear. 48 00:03:01,850 --> 00:03:03,750 So let us go for one example here. 49 00:03:04,250 --> 00:03:09,680 So here you have imported the copy model and all this is having the respective values. 50 00:03:09,680 --> 00:03:12,190 New list is able to copy copy all. 51 00:03:12,630 --> 00:03:16,460 Now here is the new list is the to copy that copy all list. 52 00:03:16,670 --> 00:03:22,550 So here you are creating the copy, printing this all list, a new list as well. 53 00:03:22,700 --> 00:03:28,090 So here is the respective output for us now, all this dot append. 54 00:03:28,130 --> 00:03:32,790 So here you are, spending some new values to this, new elements to this list. 55 00:03:33,050 --> 00:03:35,660 So now we are printing this list and the new list. 56 00:03:35,660 --> 00:03:41,210 You can easily find that this new values are getting access to are getting referred by this list. 57 00:03:41,480 --> 00:03:48,150 But the new list is only holding the references of the old values when the copy copy was issued. 58 00:03:48,410 --> 00:03:53,180 So the new values are not getting access and are not getting an effort from this new list. 59 00:03:53,660 --> 00:03:59,130 So now let us suppose let us do some adding that new nested objects, everything is in the shelf. 60 00:03:59,150 --> 00:04:04,700 Copy here we are just doing some update on this item number one one with the valuate. 61 00:04:05,000 --> 00:04:06,830 So that means there is a zero. 62 00:04:06,830 --> 00:04:07,940 That is the one. 63 00:04:07,950 --> 00:04:09,620 So one that is a zero element. 64 00:04:09,620 --> 00:04:10,550 That is the one element. 65 00:04:10,560 --> 00:04:12,860 So one man means this very element here. 66 00:04:13,400 --> 00:04:15,140 So this very element in the list. 67 00:04:15,320 --> 00:04:17,590 So it is getting updated by this eight. 68 00:04:17,960 --> 00:04:21,530 And now in the new list, I'm getting this particular value here. 69 00:04:21,770 --> 00:04:25,280 So now we can find out how these values are getting updated. 70 00:04:26,270 --> 00:04:30,680 So just I'm just copying this one to show you that if you do the update. 71 00:04:32,200 --> 00:04:36,370 At this particular place, the three one with the producer, Bebe. 72 00:04:37,630 --> 00:04:43,210 Then obviously, if you print it, you can find that we're getting this bib here, but here we are not 73 00:04:43,210 --> 00:04:47,590 getting this Beeby here and here to see we are going for this happened once again. 74 00:04:47,590 --> 00:04:53,440 So Disvalue has got upended once again, but this baby has been upgraded in this all list. 75 00:04:53,650 --> 00:04:58,150 But it is it is not there in the new year, so no send up operation will take place. 76 00:04:58,690 --> 00:05:02,320 So in this way, we will discuss what is a shallow copy. 77 00:05:02,560 --> 00:05:06,610 So actually a shallow copy does not create a copy of the nested objects. 78 00:05:06,610 --> 00:05:10,840 Instead, it just copies the reference of the nested objects. 79 00:05:10,840 --> 00:05:13,340 Only the difference of the nested objects will be copied. 80 00:05:13,780 --> 00:05:14,940 So what is happening right now? 81 00:05:14,940 --> 00:05:17,960 Nobody are doing some updates in this place. 82 00:05:17,990 --> 00:05:21,000 You are getting the respective update in the list also. 83 00:05:21,600 --> 00:05:24,070 So not let us go for the deep copy. 84 00:05:24,430 --> 00:05:25,000 No question. 85 00:05:25,030 --> 00:05:27,320 This coming in mind, what is a deep copy? 86 00:05:27,850 --> 00:05:31,600 So in case of deep copy, what happens, I'm going to discuss in the details. 87 00:05:31,870 --> 00:05:39,520 It creates a new object and recursively Airds the copies of the nested objects present in the original 88 00:05:39,520 --> 00:05:40,060 elements. 89 00:05:40,360 --> 00:05:44,340 So it's very important that in case of deep copy, we are creating a new object. 90 00:05:44,860 --> 00:05:50,640 So obviously in this case, if we print their respective ideas, the ideas will be different here. 91 00:05:50,950 --> 00:05:52,730 So let us go for the print here. 92 00:05:52,810 --> 00:05:55,900 So I'm just going for this taking this copy. 93 00:06:02,370 --> 00:06:08,010 So here to see we're having this list, having some set of elements, there's a new list, copy, dark, 94 00:06:08,010 --> 00:06:09,280 deep copy or list. 95 00:06:09,310 --> 00:06:11,220 So in this way, I'm just going for this. 96 00:06:11,220 --> 00:06:12,190 I'm just printing it. 97 00:06:12,570 --> 00:06:15,180 You can find that that new lists are getting printed. 98 00:06:16,440 --> 00:06:19,020 So now I'm going for this idea of this new list. 99 00:06:20,780 --> 00:06:21,980 And also the oldest. 100 00:06:24,940 --> 00:06:26,230 So if you go for the print. 101 00:06:27,180 --> 00:06:32,750 You're going to find that these two, or at least at least they're having their different IDs, so that 102 00:06:32,760 --> 00:06:35,580 is it has created a new object for it. 103 00:06:35,880 --> 00:06:43,590 So it creates a new object and recursively and the copies of the nested objects present in the original 104 00:06:43,860 --> 00:06:44,460 elements. 105 00:06:44,650 --> 00:06:47,820 And let's let us go for some updates here. 106 00:06:47,940 --> 00:06:52,680 So now here is the oldest one zero is getting updated by the value. 107 00:06:53,430 --> 00:06:56,250 So now you can find that we're having this update here. 108 00:06:56,950 --> 00:06:59,820 But as in the new list, we're creating a new object. 109 00:06:59,930 --> 00:07:03,780 I should not expect any update in this particular new list here. 110 00:07:04,170 --> 00:07:09,150 So I think now the conception of shallow copy and the respective deep copy it even. 111 00:07:09,150 --> 00:07:15,260 But he case of copy work or we're just calling the method there is a copy copy and include some deep 112 00:07:15,330 --> 00:07:15,690 copy. 113 00:07:15,690 --> 00:07:16,720 We're calling this method. 114 00:07:16,740 --> 00:07:18,360 There is a copy, the deep copy. 115 00:07:18,600 --> 00:07:24,990 And here in this particular demonstration, we have demonstrated that how this shallow copy and deep 116 00:07:24,990 --> 00:07:27,150 copy works in our python coding. 117 00:07:27,740 --> 00:07:28,950 Thanks for watching this video.