On Dec 10, 6:06 am, Jean-Michel Pichavant <jeanmic...@sequans.com> 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]: [1, 2, 3] > > In [5]: b > Out[5]: [5, 2, 3] > > Alternatively, you can write > > import copy > a = [1,2,3] > b = a.copy() > > if the list a contains mutable objects, use copy.deepcopy > (http://docs.python.org/library/copy.html) > > JM
I'm not a pyguru, but... you didn't use copy quite right. Try instead: b= copy.copy(a) The other issue that the original person has noticed is that a list may include a reference to something. When a list is copied - if the reference is copied (not "deepcopied"], changes to the referred object will be visible in both lists, even if they are different lists. For more information, refer to the docs in the <copy> module. HTH... -- http://mail.python.org/mailman/listinfo/python-list