Making a "deep" copy

Oliver Andrich oliver.andrich at nextra.de
Mon May 14 11:08:07 EDT 2001


Hi alex,

make use of the copy module.

Python 2.1 (#15, Apr 16 2001, 18:25:49) [MSC 32 bit (Intel)] on win32
Type "copyright", "credits" or "license" for more information.
>>> dict1 = {}
>>> dict1['a'] = [1,2,3]
>>> dict1['b'] = {}
>>> dict1['b']['first'] = [ 4,5,6]
>>> dict1
{'b': {'first': [4, 5, 6]}, 'a': [1, 2, 3]}
>>> import copy
>>> dict2 = copy.deepcopy(dict1)
>>> dict2['b']['first'][1] = 10
>>> dict1
{'b': {'first': [4, 5, 6]}, 'a': [1, 2, 3]}
>>> dict2
{'b': {'first': [4, 10, 6]}, 'a': [1, 2, 3]}
>>> 

Is this what you want?

Bye, Oliver

Monday, May 14, 2001, 4:41:14 PM, you wrote:

> Hi!

> I've got a dictionary which contains lists and other dictionaries.
> I want to create a real copy, but dict.copy() only copies the first
> "layer". Is there a (fast) way to do this? 

> Please use email for ideas. Thanks.

> Regards,
> Alex.



-- 
------------------------------------------------------------------------
Nextra Deutschland       | Oliver Andrich 
GmbH & Co. KG            | Regional Customer Care & Operations Manager 
Bornheimer Straße 26a    | Tel.:  +49 (0)228 96929-332 
53111 Bonn               | Fax:   +49 (0)228 96929-997 
                         | Mobil: +49 (0)170 7626065 
http://www.nextra.de     | E-Mail: oliver.andrich at nextra.de
------------------------------------------------------------------------ 





More information about the Python-list mailing list