copy list, which way is best? /style

Nagy László Zsolt nagylzs at freemail.hu
Wed Jul 9 14:17:58 CEST 2003


>
>
>
>from copy import copy
>l2 = copy(l1)
>
>or
>
>from copy import deepcopy
>l2 = deepcopy(l1)
>
>I don't know what the difference is, if any, but I think this way is more readable.
>

The difference between shallow and deep copying is only relevant for 
compound objects (objects that contain other objects, like lists or 
class instances):

    * A shallow copy constructs a new compound object and then (to the
      extent possible) inserts references into it to the objects found
      in the original.

    * A deep copy constructs a new compound object and then,
      recursively, inserts copies into it of the objects found in the
      original.








More information about the Python-list mailing list