replace dict contents

R

Robin Becker

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.
 
A

Andrew Bennetts

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.
 
D

Duncan Booth

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.
 
T

Tim Jarman

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.

How about:
False

HTH

Tim J
 
P

Peter Otten

Tim said:
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:

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

Peter
 
R

Robin Becker

Peter said:
Tim Jarman 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.
 
C

Christos TZOTZIOY Georgiou

Peter said:
Tim Jarman 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]
[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

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!).
 
A

Aahz

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 ([email protected]) <*> 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." (e-mail address removed)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,202
Messages
2,571,057
Members
47,665
Latest member
salkete

Latest Threads

Top