Shallow and Deep Copies Recorded Transcript This transcript was created with a speech to text software. Please excuse any typos or mismatches with the video. 11:19:36 Hi! Everybody! Welcome back to our python. Prep. Series in this video, we're gonna learn about shallow and deep copies within python. 11:19:44 Let me go ahead and share my Jupiter notebook, and we can get started. 11:19:50 So we're gonna learn about shallow and deep copies. 11:19:54 Basically, we're touching on how python handles storing data once you create it and then store it within a variable. 11:20:01 So let's start. We're going to run this code that takes the list. 11:20:05 12345678910, and then stores it in a variable called so when we execute code, when we run this code with a shift, enter what's gonna happen is this list, 1, 2, 3, 4, 5, 6, 7. 11:20:22 9, 10, it gets created and then stored somewhere in your computer's memory, and then the variable, A will then have a pointer that goes from that variable name A to the object. So visually, we can think of it looking now, sometimes you'll want to make a copy of a variable. 11:20:40 So maybe you want a different version of the variable that you want to play around with, and the hopes that you're not going to edit the original variable. 11:20:46 So in this instance we could do this by running a code like B equals A, and so in our heads, when we run B equals A, we would think, okay, now, I have a variable, A that has the list one through 10 in it. And A variable B that also has its own, unique list. 11:21:03 With one through 10 of it. But what is going on is what's called python assignment. 11:21:09 So when you assign a an existing variable to a new variable in python, so like, be equals. A, what's going on is B, instead of creating a brand new object behind the scenes in your computer, both A and B are pointing to the same thing within that computer, so if we did something 11:21:27 like B at 4. So changing the 4 entry of B to 11, we're actually gonna not just alter B which we can now see is 1, 2, 3, 4, 11, 6. 11:21:35 We're also altering a why is that? 11:21:40 Because both A and B are pointing to the same object within your computers, memory. 11:21:46 So one way we can check that. They're pointing to the same object is the Id function from base python. 11:21:54 Here's a link to the documentation on that. So when we run Id of X what we're going to get is a unique integer for the objects stored within a, which is this list, one through 10. 11:22:06 Now, it's a list 1, 2, 3, 4 11, 6, 7, 8, 9, 10, so this is the Id for the list stored within a. 11:22:14 But if we also do the id of B, we can see that it is the exact same integer. 11:22:18 So a new object was not created when we did. B equals A instead B just pointed to the same thing that A was. 11:22:25 So, how do we get a true new object when we want to make a copy of something? 11:22:31 So we want a copy of what is in A, but not pointing to the exact same thing that a points to. 11:22:39 So the way to do that is to use the copy module. So this is one way. There are other ways. 11:22:46 But we're gonna stick to the copy module cause it works for most of the objects within python so we're gonna talk a little bit more about importing a module or package. 11:22:52 In Python in a later Jupiter notebook, slash Lecture. 11:22:56 So, for now just trust me, we're gonna import the copy module. 11:23:02 We're going to show you how to use it. And then, if you want to understand more about how to import a package or a module within python, check out that lecture. 11:23:12 So in order to do that, we're going to write the code. So we've reassigned a the list one through 10. 11:23:27 And so now a will point to this list, one through 10. 11:23:31 Now we're going to do instead of B equals A, we're going to do the copy module dot the function copy and then put a in here. 11:23:36 So now I've made what is known as a shallow copy of A, which is going to create a new list, object, copy and paste over all the entries from the original list object. 11:23:49 So now we have. If I change something about B like changing the fourth element to the number 11. 11:23:58 Now A is the same, but B is altered, and we can do a quick check. 11:24:03 Well, what about the ids of A and B. The Ids are different. 11:24:07 So if you look at the Ids, they're very close, but they are different. 11:24:11 So? Why is that so? When we created a this new object was created, the list one through 10. 11:24:18 Then, when I caught me a shallow copy of a a new list object was created for B, and then all the contents of A were just quickly copy and paste it over. 11:24:31 So that's why these are 2 different ids. These are 2 different objects. 11:24:35 And if you're interested I keep saying the word object a lot. Maybe you don't know what that means. 11:24:43 If you have time, check out the classes and objects. 11:24:46 Optional notebook. After you go through the rest of the python so here, you know, we think that we've done a great new job. 11:24:51 We're doing great with A and B and shallow copies, but there isn't issue of shallow copies, meaning they struggle with how they copy the interior of an object when you have more complicated data structures. 11:25:06 So instead of a list of integers, what if we had a list of lists? So A is now going to be the list. 11:25:12 The list of one comma, the list of 2 comma, the list of 3 comma, the list of 4 comma, the list of 5. 11:25:19 So what do we? And if we do a shallow copy of that, what's gonna happen? If, for instance? 11:25:22 Well, I still want the 0 entry of B to be a list, but I want the 0 entry of that list to be a 100 so maybe I reassign this one to be a 100. 11:25:30 I just did a copy. So I shouldn't change a when I change B. 11:25:37 But lo and behold the way that a was change the way I changed B also impact. 11:25:41 So this is an issue with copy dot, copy, a shallow copy is that it's not copyying, making unique copies of the things within a. 11:25:54 So somehow in your memory, these lists 1, 2, 3, 4, and 5 have their own places in your memory, and when you make a shallow copy you're just pointing to those things. 11:26:04 So we can do this by checking. If I look at the Id of the 0 entry of A, which is this list? 11:26:11 What used to be one. Now it's the list 100 it's the same as the Id of the 0 entry of B. 11:26:17 So how do I make a copy that does a true copy of all the stuff on the inside that is called a deep copy? 11:26:25 So in order to make a copy of A that truly makes unique new copies of the things within A, I have to do copyright deep copy. 11:26:33 So now, when I change the 0 entry of the 0 entry of B to be installed of 1 100 a is not changed, but B is changed. 11:26:46 So that's why, in this sort of case, when you have a complicated data structure, you want to use a deep copy instead of a shallow copy. 11:26:57 If you had a list that you want to copy, that is just integers. You can just use a shallow copy, and you'll be fine. But when you have like something that's a compound object. 11:27:06 So lists of lists, Tuples of Tuples, you wanna make a deep cop. 11:27:10 So maybe you're gone through all this. We're done with comies, and maybe we're done with A, we don't want to use A or B anymore. 11:27:19 So how can I get rid of a variable within python with the D. E. L. 11:27:25 Which stands for delete statement. So if you do, de L. 11:27:29 And then. Now, when I reference a, it will be gone. 11:27:32 Okay. So now the pointer to that object is deleted, the object itself may exist in your memory for a little bit, but since nothing's pointing to it, it will eventually get deleted from your memory, so that list of lists will eventually go away. 11:27:50 And to demonstrate how you can use Delete to get rid of multiple things. 11:27:53 I re-readan this code. So both A and B now exist again. 11:27:58 So if you want to get rid of both A and B in the same line, you can just separate them by a comma. And now both of them will be deleted. 11:28:05 So, and then also, B is gone. Okay, all right. So that is it for shallow and deep copies you might be wondering, why are we learning about that? This seems silly? 11:28:20 So when you're doing a lot of data manipulation later on, if you're doing the data science boot camp, you're gonna want to make things like shallow copies and deep copies of. 11:28:27 Some of the pandas data structures, and again, pandas is something you'll learn about again later. 11:28:32 So I just want you to have an understanding of when you do something like B equals A, that's just assignments. 11:28:41 And you're actually not making a unique copy of something. 11:28:43 So if you did something to be that alters what's within? 11:28:48 B, you're also altering what's within a okay?