copying a list
Alex Martelli
alex at magenta.com
Tue Aug 8 12:25:02 EDT 2000
"Gert-Jan Hovinga" <lance_99_99 at yahoo.com> wrote in message
news:39904406.9DCD282 at yahoo.com...
> is there an easy way to get a copy of a list instead of a reference?
Yes:
import copy
a=[1,2]
b.append(copy.copy(a))
a.pop()
print b
will print [[1,2]] as you desire. Also see copy.deepcopy for
harder but similar tasks:-).
For the specific case of a list,
b.append(a[:])
also works fine. But copy.copy is more general.
Alex
More information about the Python-list
mailing list