newbie: copying dictionaries
Erik Max Francis
max at alcyone.com
Wed Jun 12 00:43:30 EDT 2002
"Jon J. Morin" wrote:
> Hi. I'm a newbie to python and have a question about copying
> dictionary
> objects. I'm working out of Learning Python by Mark Lutz & David
> Ascher
> (O'Reilly). 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?
With the assignment statement:
newDict[key] = aDict[key]
This assigns the key in the new dictionary to the value of the old.
Note you could shorten the function using the .update method:
def copyDict(aDict):
newDict = {}
newDict.update(aDict)
return newDict
--
Erik Max Francis / max at alcyone.com / http://www.alcyone.com/max/
__ San Jose, CA, US / 37 20 N 121 53 W / ICQ16063900 / &tSftDotIotE
/ \ Who'd ever think it / Such a squalid little ending
\__/ The American and Florence, _Chess_
Church / http://www.alcyone.com/pyos/church/
A lambda calculus explorer in Python.
More information about the Python-list
mailing list