newbie: copying dictionaries

Emile van Sebille emile at
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
> copied from one object to the other, but how do the values get copied
> 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


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

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



Emile van Sebille
emile at


More information about the Python-list mailing list