473,324 Members | 2,214 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,324 software developers and data experts.

dictionary.copy()?

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
1 1571
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Tobias Pfeiffer | last post by:
Hi! Damnit, am I angry! Please look at the following example: class bla: def __init__(self): self.ho = {'a': , 'c': , 'b': , 'e': , 'd': , 'g': , 'f': , 'h': } # shall be an adjacence list...
57
by: Egor Bolonev | last post by:
why functions created with lambda forms cannot contain statements? how to get unnamed function with statements?
125
by: Raymond Hettinger | last post by:
I would like to get everyone's thoughts on two new dictionary methods: def count(self, value, qty=1): try: self += qty except KeyError: self = qty def appendlist(self, key, *values): try:
4
by: Betina Andersen | last post by:
I have a dictionary object, then I create a new dictionary object and sets it equal to my original, then I pass the new dictionary object to a function that changes some of my values - but then my...
18
by: Marko.Cain.23 | last post by:
Hi, I create a dictionary like this myDict = {} and I add entry like this: myDict = 1 but how can I empty the whole dictionary? Thank you.
0
by: DaTurk | last post by:
Hi, I have a generic dictionary defined, and when I attpet to return a reference to it via a property I get this error error C3073: 'System::Collections::Generic::Dictionary<TKey,TValue>' : ref...
3
by: JamesB | last post by:
I have a config screen in my app that updates a dictionary<key,value>. I want to store a copy of this before going into the config screen so if the user wants to cancel all their changes I can...
8
by: Bob Altman | last post by:
Hi all, I'm trying to do something that should be really easy, but I can't think of an obvious way to do it. I have a dictionary whose value is a "value type" (as opposed to a reference type --...
3
by: mk | last post by:
Hello everyone, I'm storing functions in a dictionary (this is basically for cooking up my own fancy schmancy callback scheme, mainly for learning purpose): .... return "f2 " + arg .......
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.