Working with copies of a dictionary

Duncan Smith buzzard at urubu.freeserve.co.uk
Fri Dec 12 19:44:52 EST 2003


[snip]
>
> Not even the slightest connection is not quite right.
>
> >>> adict = {'a':[1,2,3], 'b':[3,4,5]}
> >>> anotherdict = adict.copy()
> >>> id(adict['a'])
> 10410608
> >>> id(anotherdict['a'])
> 10410608
> >>>
>
> adict['a'] and anotherdict['a'] reference the same list.  Try a deep copy.
>
> >>> import copy
> >>> a_third_dict = copy.deepcopy(adict)
> >>> a_third_dict
> {'a': [1, 2, 3], 'b': [3, 4, 5]}
> >>> id(adict3['a'])
> 10411984
>
> Duncan
>

Of course I shouldn't edit output once I've pasted it.  That should be,

>>> id(a_third_dict['a'])
10411984






More information about the Python-list mailing list