newbie: copying dictionaries

Emile van Sebille emile at fenx.com
Wed Jun 12 00:44:41 EDT 2002


Jon J. Morin
> The following bit of code was an exercise in the book and it
> works, but what I'm asking is why?  I see where the dictionary keys
get
> copied from one object to the other, but how do the values get copied
over
> as well?

> def copyDict(aDict):
>     newDict = {}
>     for key in aDict.keys():
>         newDict[key] = aDict[key]

This statement does both:

    newDict[key] ... creates key in newDict

and

    = aDict[key] ... places the object pointed at in it


>>> d = {1:11,2:22,3:33}
>>>
>>> d[1]
11

HTH,

--

Emile van Sebille
emile at fenx.com

---------




More information about the Python-list mailing list