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
--
http://mail.python.org/mailman/listinfo/python-list

Reply via email to