Copy List

Marc 'BlackJack' Rintsch bj_666 at gmx.net
Thu Jul 19 13:04:00 EDT 2007


On Thu, 19 Jul 2007 09:21:54 -0700, Falcolas wrote:

> On Jul 18, 6:56 am, "Rustom Mody" <rustompm... at gmail.com> wrote:
>> This is shallow copy
>> If you want deep copy then
>> from copy import deepcopy
> 
> What will a "deep copy" of a list give you that using the slice
> notation will not?

Well, a deep copy of course.  ;-)

In [6]: import copy

In [7]: a = [[1, 2], [3, 4]]

In [8]: b = a[:]

In [9]: c = copy.deepcopy(a)

In [10]: a[0][1] = 42

In [11]: a
Out[11]: [[1, 42], [3, 4]]

In [12]: b
Out[12]: [[1, 42], [3, 4]]

In [13]: c
Out[13]: [[1, 2], [3, 4]]

Ciao,
	Marc 'BlackJack' Rintsch



More information about the Python-list mailing list