Python: How to copy lists of list or lists of objects

python copy list
This is a topic that many people are looking for. is a channel providing useful information about learning, life, digital marketing and online courses …. it will help you have an overview and solid multi-faceted knowledge . Today, would like to introduce to you Python: How to copy lists of list or lists of objects. Following along are instructions in the video below:

hi Mikes dish here and I wanted to show you a gotcha that I ran into in Python it would be quite a while to figure out why a program I was working on that generates QR codes was getting like erroneous data when I was looping through some of my lists and you know Ive Ive used Python for some time and I felt I kind of knew all of the ins and outs of it but this one really threw me for a loop so hopefully itll be helpful to you so it has to do with lists so lets just go ahead and make ourselves a test list and what Im going to do is make several members and you know it doesnt really need to be anything important for this example you know I just want to be able to tell what each part of the list is probably have spaces here just to make things look good follow the conventions okay so that should be it so what does that look like to you to me that looks like a list of Lists so we have you know these open and closed brackets that are the list itself and then each of these members has brackets which makes them all this so we have a list of Lists on you know if you print it out you get what you get and now lets make a copy of that list so lets just call this list copy equals test list alright so and lets copy has the same you know contains the same thing you probably already know what Im gonna say here lets just go ahead and define on a little function here to display some IDs lets print list ID and well do ID test list okay list copy ID okay so this ID function thats right here what this is going to do is its going to give us the like its basically a memory location for where that list is stored and if we run this check this out we have exactly the same

number here so we have two different variables but theyre pointed it to exactly the same data and the problem with that is if I change data in one list its also going to change the data in the other list and I dont want to do that so the actual right way to make a cut to have made a copy would have been for us to assign the list by calling the list function so the constructor in this list function is going to make a brand new list and its gonna store it as list copy so now if we display our IDs we get different numbers of different memory locations which is what we want now I understood that I was using that Im making a copy of the list its just fine but I was still getting erroneous data and let me show you a little example of why so if I go ahead and I take one of the if I take lets say the first member of the first member of my original list and I change it to big trouble and I display test list you can see there it says big trouble but now watch this if I display list copy I also get big trouble and the reason is Python doesnt see this as lists of lists Python sees this as lists of list objects and so when I made the copy it was copying the memory location of the object so lets lets get my definition back up here and lets add some more stuff so lets print on the idea of the first member of each list lets just go ahead and copy this okay so if we now if we display the ID youll see that although we have different memory locations for each of our lists our overall lists the list inside of each list are pointing to the same memory location now theres a couple of different ways that we could have avoided this so for instance we could have done on some kind of iteration through the

where we you know youve basically in closed you know like you make a list copy just be a list and then you say for item in test list and then list copy dot append and then something like this now this is going to give us our same members again but its going to have different IDs because weve actually gone through and verbose we said make a list so this is fine this is one way to do it but the problem is if you dont actually know if the inside lists can themselves contain objects because any time a list inside of it is going to contain an object its going to copy the memory address of that object its not actually going to actually copy a new list and so one way that we can get around this you know if you dont know for sure you know you dont want to make the mistake enough to track this bug down again is by using the built in deep copy so I would probably do something like import or from copy import deep copy but for now Im just going to do a whole import import copy and so what we can do here lets just go ahead and delete the list copy so we know were starting from so list copy does not exist so heres what were gonna do were gonna do list copy equals copy dot deep copy and then our original list which is called test now we get a copy of that and remember big trouble was in there because we actually change that in the first list but if we display our IDs at this point we do have different IDs for the overall lists and then different IDs for on the members within those lists so deep copy something youre going to use if youre copying list of lists or lists of objects so you can avoid the same gotcha that I encountered once again my name is Mikes dish you can read more about this at jump tuck com thanks for watching

python, copy, deepcopy, list, object, gotcha
Thank you for watching all the articles on the topic Python: How to copy lists of list or lists of objects. All shares of are very good. We hope you are satisfied with the article. For any questions, please leave a comment below. Hopefully you guys support our website even more.

Leave a Comment