Hi, This web is quite useful to visualize what is happening: http://www.pythontutor.com/visualize.html#mode=edit
Step by Step: >>> a=[1,2] You create a list a which contains two objects, in this case two integers (1, 2) >>> l=[a,a] You create a list which contains two objects, which happen to be the list above created. This is just a reference to the object (1,2) same as list a is referring to. >>> l[0]=[0,0] You modify the first element of the list to another list, now the reference has been modified and it points to the new list [0, 0] This is why the id of l[0] changes because you are referencing to another object which has another id. l[1] is still referencing to the list [1,2] same as a this is why the id for a and l[1] doesn't change. Take a look on the memory visualize tool which I think is helpful to understand what is going on in this cases. Kind Regards, Raul On Wed, Nov 26, 2014 at 12:57 PM, Mohamed Ben Mosbah < benmosbahmoha...@gmail.com> wrote: > Hi I'm new to Python and I would like to know how he deals with memory > space. > I thought I had understood but I made a test and the results were > uncoherenent with my understanding, here is the thing: > > >>> a=[1,2] > >>> l=[a,a] > >>> id(a); id(l[0]); id(l[1]); > 61659528 > 61659528 > 61659528 > >>> #All Have the same ID > >>> l[0]=[0,0] > >>> l > [[0, 0], [1, 2]] > >>> #Why didn't l[1] change as well? > >>> id(a); id(l[0]); id(l[1]); > 61659528 > 61390280 > 61659528 > >>> #How come id(l[0]) changed? > >>> a = [4,4] > >>> l > [[0, 0], [1, 2]] > >>> #Why didn't l[1] change? l[1] and a were occupying the same memory > adress! > > > Thank you for answering :) > > _______________________________________________ > Tutor maillist - Tutor@python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor > >
_______________________________________________ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor