By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,264 Members | 1,743 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,264 IT Pros & Developers. It's quick & easy.

dictionary.copy()?

P: n/a
Here is some example code:

d = {"a":"hello", "b":[1, 2, 3]}

x = d.copy()
d["b"][0]=10
print x

output:
{'a': 'hello', 'b': [10, 2, 3]}

It looks like the key names of a dictionary store pointers to the
values? Or does a dictionary object manage pointers to keys and
values, so copy() above just copies 4 pointers?

Mar 21 '07 #1
Share this Question
Share on Google+
1 Reply


P: n/a
7stud <bb**********@yahoo.comwrote:
Here is some example code:

d = {"a":"hello", "b":[1, 2, 3]}

x = d.copy()
d["b"][0]=10
print x

output:
{'a': 'hello', 'b': [10, 2, 3]}

It looks like the key names of a dictionary store pointers to the
values? Or does a dictionary object manage pointers to keys and
values, so copy() above just copies 4 pointers?
dict objects hold references (roughly the same as C pointers) to keys
and values, and dict.copy does a shallow copy. If you want a deep copy,
import copy and x=copy.deepcopy(d) instead.

Deep copies are very costly (because they copy a lot of data bits rather
than just references), so it's quite sensible to have to ask for one
very specifically in the rare cases in which you really want it.
Alex

Mar 21 '07 #2

This discussion thread is closed

Replies have been disabled for this discussion.