473,508 Members | 2,330 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

Similar topics

2
4896
by: Leif K-Brooks | last post by:
For my web-based application, I need to make twelve different calls to str.replace for the content of /every/ page on /every/ page view (or find a harder to implement and probably less elegant...
8
1412
by: OPQ | last post by:
Hi all, I'd happy to have you share some thougts about ultimate optimisations on those 2 topics: (1)- adding one caractere at the end of a string (may be long) (2)- in a dict mapping a key...
7
3083
by: Marcio Rosa da Silva | last post by:
Hi! In dictionaries, unlinke lists, it doesn't matter the order one inserts the contents, elements are stored using its own rules. Ex: >>> d = {3: 4, 1: 2} >>> d {1: 2, 3: 4}
19
2906
by: rbt | last post by:
Here's the scenario: You have many hundred gigabytes of data... possible even a terabyte or two. Within this data, you have private, sensitive information (US social security numbers) about your...
8
4019
by: Eric Lilja | last post by:
Hello, I had what I thought was normal text-file and I needed to locate a string matching a certain pattern in that file and, if found, replace that string. I thought this would be simple but I had...
3
2038
by: Bengt Richter | last post by:
Has anyone found a way besides not deriving from dict? Shouldn't there be a way? TIA (need this for what I hope is an improvement on the Larosa/Foord OrderedDict ;-) I guess I can just document...
11
2050
by: sandravandale | last post by:
I can think of several messy ways of making a dict that sets a flag if it's been altered, but I have a hunch that experienced python programmers would probably have an easier (well maybe more...
7
3123
by: manstey | last post by:
Hi, How do I convert a string like: a="{'syllable': u'cv-i b.v^ y^-f', 'ketiv-qere': 'n', 'wordWTS': u'8'}" into a dictionary: b={'syllable': u'cv-i b.v^ y^-f', 'ketiv-qere': 'n', 'wordWTS':...
6
3082
by: =?Utf-8?B?R2Vvcmdl?= | last post by:
Hello, I have some XML that is returned to my application from another vendor that I cannot change before it gets to me. I can only alter it after it gets to my application. That being said, I...
0
7410
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
7067
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7505
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5650
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5060
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...
0
4729
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3215
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
1
774
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
440
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.