Dict Copy & Compare

Steven D'Aprano steve at REMOVEME.cybersource.com.au
Mon Apr 30 06:08:51 EDT 2007


On Mon, 30 Apr 2007 09:40:53 +0100, Tim Golden wrote:

> Robert Rawlins - Think Blue wrote:
>> I'm looking for a little advice on dicts, firstly I need to learn how to
>> copy a dict, I suppose I could just something like.
> 
>> Self.newdict = self.olddict
> 
>> But I fear that this only creates a reference rather than an actual copy,
>> this means that as soon as I clear out the old one, the new one will
>> effectively be empty. What's the best way to ACTUALY copy a dict into a new
>> variable?
> 
> Unless you have specialised needs, you can just say:
> 
> d2 = dict (d1)



Or you can say 

d2 = d1.copy()



-- 
Steven D'Aprano 




More information about the Python-list mailing list