copying a list

Ruud de Rooij * at spam.ruud.org
Tue Aug 8 11:39:54 EDT 2000


Gert-Jan Hovinga <lance_99_99 at yahoo.com> writes:

> is there an easy way to get a copy of a list instead of a reference?
> 
> for example, the following code prints [[1]] :
> 
> a = [1, 2]
> b = []
> b.append(a)
> a.pop()
> print b
> 
> but i want it to print [[1, 2]]
> 
> any easy solutions? or do i have to write a copy function myself?

Use b.append(a[:]) instead.

	- Ruud de Rooij.
-- 
ruud de rooij | *@spam.ruud.org | http://ruud.org



More information about the Python-list mailing list