469,903 Members | 1,610 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,903 developers. It's quick & easy.

replace dict contents

Is there a simple way to replace the contents of a dictionary entirely
with those of another.

for lists we can do

L1[:] = L2

but there doesn't seem to be an equivalent for dicts.
--
Robin Becker
Jul 18 '05 #1
7 1455
On Tue, Jul 27, 2004 at 08:56:35AM +0100, Robin Becker wrote:
Is there a simple way to replace the contents of a dictionary entirely
with those of another.

for lists we can do

L1[:] = L2

but there doesn't seem to be an equivalent for dicts.


d1.clear()
d1.update(d2)

-Andrew.

Jul 18 '05 #2
Robin Becker <ro***@SPAMREMOVEjessikat.fsnet.co.uk> wrote in
news:41**************@jessikat.fsnet.co.uk:
Is there a simple way to replace the contents of a dictionary entirely
with those of another.

for lists we can do

L1[:] = L2

but there doesn't seem to be an equivalent for dicts.


The simplest would seem to be

d1.clear()
d1.update(d2)

although, this doesn't have the flexibility of a single assignment so you
can't use it where 'd2' is actually an expression involding d1.
Jul 18 '05 #3

On 27 Jul 2004, at 08:56, Robin Becker wrote:
Is there a simple way to replace the contents of a dictionary entirely
with those of another.

for lists we can do

L1[:] = L2

but there doesn't seem to be an equivalent for dicts.
--
Robin Becker
--
http://mail.python.org/mailman/listinfo/python-list


How about:
d1 = { "spam" : "eggs" }
d2 = { "gumby" : "my brain hurts!"}
d1 is d2 False d1 = dict(d2)
d1 {'gumby': 'my brain hurts!'} d1 is d2

False

HTH

Tim J

Jul 18 '05 #4
Tim Jarman wrote:
On 27 Jul 2004, at 08:56, Robin Becker wrote:
Is there a simple way to replace the contents of a dictionary entirely
with those of another.

for lists we can do

L1[:] = L2
How about:
>>> d1 = { "spam" : "eggs" }
>>> d2 = { "gumby" : "my brain hurts!"}
>>> d1 is d2 False >>> d1 = dict(d2)


This rebinds the name d1 to a copy of d2 and will not affect other
references to the original d1.

Peter
Jul 18 '05 #5
Peter Otten wrote:
Tim Jarman wrote:

On 27 Jul 2004, at 08:56, Robin Becker wrote:

Is there a simple way to replace the contents of a dictionary entirely
with those of another.

for lists we can do

L1[:] = L2

How about:
>>> d1 = { "spam" : "eggs" }
>>> d2 = { "gumby" : "my brain hurts!"}
>>> d1 is d2

False
>>> d1 = dict(d2)

This rebinds the name d1 to a copy of d2 and will not affect other
references to the original d1.

Peter

Peter has the essence of the problem. Attempts to change sys.modules
have strange effects eg try this simple script

import sys
omods = sys.modules
sys.modules = omods.copy()

print 'start', len(sys.modules), len(omods)
import urlparse
print 'after import', len(sys.modules), len(omods)
In my executions it's len(omods) that changes
so we need a way to copy the original sys.modules and then quickly
replace uf we want to restore the original value.

The actual replace part of L1[:]=L2 happens in a single opcode and is
therefore atomic. The same cannot be said of the .clear, .update sequence.
--
Robin Becker
Jul 18 '05 #6
In article <41**************@jessikat.fsnet.co.uk>,
Robin Becker <ro***@SPAMREMOVEjessikat.fsnet.co.uk> wrote:

The actual replace part of L1[:]=L2 happens in a single opcode and is
therefore atomic. The same cannot be said of the .clear, .update sequence.


Why do you care about atomicity? Are you running a threaded app?
--
Aahz (aa**@pythoncraft.com) <*> http://www.pythoncraft.com/

"To me vi is Zen. To use vi is to practice zen. Every command is a
koan. Profound to the user, unintelligible to the uninitiated. You
discover truth everytime you use it." --*****@lion.austin.ibm.com
Jul 18 '05 #7
On Tue, 27 Jul 2004 22:10:42 +0100, rumours say that Robin Becker
<ro***@SPAMREMOVEjessikat.fsnet.co.uk> might have written:
Peter Otten wrote:
Tim Jarman wrote:

On 27 Jul 2004, at 08:56, Robin Becker wrote:
[Robin]Is there a simple way to replace the contents of a dictionary entirely
with those of another.

for lists we can do

L1[:] = L2
[Tim J]How about:

>>> d1 = { "spam" : "eggs" }
>>> d2 = { "gumby" : "my brain hurts!"}
>>> d1 is d2
False
>>> d1 = dict(d2)
[Peter]
This rebinds the name d1 to a copy of d2 and will not affect other
references to the original d1.

[Robin]Peter has the essence of the problem. Attempts to change sys.modules
have strange effects eg try this simple script
[snip]
The actual replace part of L1[:]=L2 happens in a single opcode and is
therefore atomic. The same cannot be said of the .clear, .update sequence.


A compromise, then, which might be good enough: first update, then
remove all missing keys in two operations. ie
d1.update(d2)
list(itertools.ifilter(None, itertools.imap(d1.__delitem__, set(d1)-set(d2))))


An 'import itertools' is implied. As soon as the keys to be deleted are
calculated, the rest of the second operation is atomic (I believe!).
--
TZOTZIOY, I speak England very best,
"Tssss!" --Brad Pitt as Achilles in unprecedented Ancient Greek
Jul 18 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Leif K-Brooks | last post: by
7 posts views Thread by Marcio Rosa da Silva | last post: by
6 posts views Thread by =?Utf-8?B?R2Vvcmdl?= | last post: by
1 post views Thread by Waqarahmed | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.