Re: decouple copy of a list

2010-12-10 Thread Ben Finney
Dirk Nachbar writes: > I want to take a copy of a list a > > b=a In addition to the other good replies you've received: To take a copy of an object, the answer is never ‘b = a’. That binds a reference ‘b’ to the same object referenced by ‘a’. The assignment operator ‘=’ never copies; it binds

Re: decouple copy of a list

2010-12-10 Thread Jean-Michel Pichavant
cassiope wrote: Alternatively, you can write import copy a = [1,2,3] b = a.copy() JM I'm not a pyguru, but... you didn't use copy quite right. Try instead: b= copy.copy(a) You're right, you're not a python guru so don't even try to contradict me ever again. ... :D of course I di

Re: decouple copy of a list

2010-12-10 Thread nn
On Dec 10, 8:48 am, Dirk Nachbar wrote: > I want to take a copy of a list a > > b=a > > and then do things with b which don't affect a. > > How can I do this? > > Dirk Not knowing the particulars, you may have to use: import copy b=copy.deepcopy(a) -- http://mail.python.org/mailman/listinfo/pyt

Re: decouple copy of a list

2010-12-10 Thread cassiope
On Dec 10, 6:06 am, Jean-Michel Pichavant wrote: > Dirk Nachbar wrote: > > I want to take a copy of a list a > > > b=a > > > and then do things with b which don't affect a. > > > How can I do this? > > > Dirk > > In [1]: a = [1,2,3] > > In [2]: b = a[:] > > In [3]: b[0] = 5 > > In [4]: a > Out[4]:

Re: decouple copy of a list

2010-12-10 Thread Wolfgang Rohdewald
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 ar

Re: decouple copy of a list

2010-12-10 Thread Dirk Nachbar
On Dec 10, 1:56 pm, Wolfgang Rohdewald wrote: > On Freitag 10 Dezember 2010, Dirk Nachbar wrote: > > > I want to take a copy of a list a > > > b=a > > > and then do things with b which don't affect a. > > > How can I do this? > > > Dirk > > b=a[:] > > -- > Wolfgang I did that but then some things

Re: decouple copy of a list

2010-12-10 Thread Jean-Michel Pichavant
Dirk Nachbar wrote: I want to take a copy of a list a b=a and then do things with b which don't affect a. How can I do this? Dirk In [1]: a = [1,2,3] In [2]: b = a[:] In [3]: b[0] = 5 In [4]: a Out[4]: [1, 2, 3] In [5]: b Out[5]: [5, 2, 3] Alternatively, you can write import copy a

Re: decouple copy of a list

2010-12-10 Thread Wolfgang Rohdewald
On Freitag 10 Dezember 2010, Dirk Nachbar wrote: > I want to take a copy of a list a > > b=a > > and then do things with b which don't affect a. > > How can I do this? > > Dirk b=a[:] -- Wolfgang -- http://mail.python.org/mailman/listinfo/python-list

Re: decouple copy of a list

2010-12-10 Thread Daniel Urban
b = list(a) or b = a[:] -- http://mail.python.org/mailman/listinfo/python-list

Re: decouple copy of a list

2010-12-10 Thread Kushal Kumaran
On Fri, Dec 10, 2010 at 7:18 PM, Dirk Nachbar wrote: > I want to take a copy of a list a > > b=a > > and then do things with b which don't affect a. > > How can I do this? > b = a[:] will create a copy of the list. If the elements of the list are references to mutable objects (objects of your ow

decouple copy of a list

2010-12-10 Thread Dirk Nachbar
I want to take a copy of a list a b=a and then do things with b which don't affect a. How can I do this? Dirk -- http://mail.python.org/mailman/listinfo/python-list