decouple copy of a list
Jean-Michel Pichavant
jeanmichel at sequans.com
Fri Dec 10 09:06:18 EST 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
>
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
More information about the Python-list
mailing list