decouple copy of a list

Wolfgang Rohdewald wolfgang at rohdewald.de
Fri Dec 10 09:36:01 EST 2010


On Freitag 10 Dezember 2010, Dirk Nachbar wrote:
> > b=a[:]
> > 
> > --
> > Wolfgang
> 
> I did that but then some things I do with b happen to a as
> well.

as others said, this is no deep copy. So if you do something
to an element in b, and if the same element is in a, both
are changed as they are still the same objects:

>>> x,y=5,6
>>> a=[x,y]
>>> b=a[:]
>>> id(a),id(b)
(140695481867368, 140695481867512)
>>> id(a[0]),id(b[0])
(33530584, 33530584)
>>> a=b
>>> id(a),id(b)
(140695481867512, 140695481867512)


-- 
Wolfgang



More information about the Python-list mailing list