Duplicating list of lists [newbie]
Ryan Ginstrom
software at ginstrom.com
Sun Mar 23 11:46:17 EDT 2008
> On Behalf Of yatsek at gmail.com
> So - it looks that in list "b" there copy of all objects from list "a"
> including not copy of list [5,6,7] but reference to it.
>
> Is there simple way to copy a into b (like a[:]) with all
> copies of all objects going as deep as possible? Or it can be
> done only manually?
I'd suggest checking out copy.deepcopy.
>>> a = [1, [1, 2, 3], 2]
>>> b = a[:]
>>> a[1][2] = 'spam'
>>> b
[1, [1, 2, 'spam'], 2]
>>> from copy import deepcopy
>>> b = deepcopy(a)
>>> a[1][2] = 'deepcopy is your friend'
>>> b
[1, [1, 2, 'spam'], 2]
Regards,
Ryan Ginstrom
More information about the Python-list
mailing list