Ask your own question, for FREE!
Computer Science 22 Online
OpenStudy (anonymous):

For Python: I'm trying to replicate a list of lists. This function takes in a list and is supposed to return a duplicate. However, it just gives me a duplicate reference to the list, rather than a list independent of the first one. def getListCopy(list): dupeList = [] for x in list: dupeList.append(x) return dupeList Can anyone explain why this returns a list reference instead of a duplicate for the list values?

OpenStudy (anonymous):

you have a list of objects a = [object1, object2] - here your objects are lists b = a[:] will create a new list, containing the same objects (lists) the simplest way around this is to use deepcopy - from copy import deepcopy dupList = deepcopy(a) it copies copies of the objects in a ( google deepcopy in python for better explanations...)

OpenStudy (rsmith6559):

Most languages pass primitives ( int, float, bool, char, maybe string ) by value. Objects and collections are passed by reference because they can be large and take a while to copy and really suck up memory.

OpenStudy (anonymous):

Right, I understand that objects are passed by reference. I guess my question is, why would something like def getListCopy(list): dupeList = [] for x,y in list: dupeList += [[x,y]] return dupeList work for a list of lists rather than my first post? Here, my objects are still lists and my new list contains all the same objects of my old list. This function returns a duplicate list independent of the first one. That is to say, if dupeList=getListCopy(list), list != dupeList. In my first post, dupeList = list. Anything I changed in list, I changed in dupeList. Why is this?

OpenStudy (anonymous):

a = [[1,2],[2,3],[3,4]] a is a list of references to lists a[0] references the list [1,2], that is, a[0] is the address which contains the list [1,2] so in your original question, dupList was copying the references to the lists, not the lists themselves. what you can do is: d=[] for x in a: ...e = [] ...for y in e: ......e.apend[y] ...d.append[e] but maybe i've misunderstood (again).

OpenStudy (anonymous):

eeech...for y in x:

OpenStudy (anonymous):

Okay there we go I get it. This is the response I was looking for. In my original function, I was just appending the reference to the lists...that's why if I changed anything in list, I was also changing it in dupeList. When you explained it the first time, I thought you were just explaining the concept of references to lists (which I get). I should have sent you the second function the first time, because what I really wanted to know was why append() didn't work (it copied references). So thanks! I also looked into deepcopy. It's really useful

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!