Copy List

James Matthews nytrokiss at gmail.com
Thu Jul 19 19:14:53 CEST 2007


A slice still has some references to the old objects a deep copy is a
totally new object!

On 19 Jul 2007 17:04:00 GMT, Marc 'BlackJack' Rintsch <bj_666 at gmx.net>
wrote:
>
> 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
> --
> http://mail.python.org/mailman/listinfo/python-list
>



-- 
http://www.goldwatches.com/Watches.asp?Brand=14
http://www.jewelerslounge.com
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20070719/31cf4363/attachment.html>


More information about the Python-list mailing list