Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
replacing dict contents
#1
for a reference to a list, such as a global list, i can replace all the contents like this:
mylist[:] = newlist
for a reference to a dict. such as a global dict, i can replace certain contents like this:
mydict.update(newdict)
but this is not a total replacement. keys in the referenced dict that are not in the new dist will remain in the referenced dict with their original value(s). it is possible to do a full content replacement by clearing the dict before the update:
mydict.clear()
mydict.update(newdict)
or by name assignment:
global mydict
. . .
mydict = newdict
my question is: is there a way to completely replace the dict contents, from another dict, in one single operation as can be done with a list?
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#2
As far as I know, it is not possible. What's wrong with doing it in two operations instead of one?

A very long time ago, there was a nice module kjbuckets in Python 1 and 2 by Aaron Watters, one of the early adopters and apostles of Python and it had interesting operations for kjDicts such as algebraic operations (sum, difference, products). It could be interesting to have a modernized version of this API for todays Python. I guess it's worth browsing Pypi's packages to find original dictionary APIs.

Note: you can still try kjbuckets on Python 2 by downloading the gadfly database here. Gadfly won't run out of the box, but it contains kjbuckets which seems to be working. You need a C compiler. On Ubuntu, install libpython2-dev to compile.

Edit: finally, I couldn't find a way to do the exact update that you want with kjbuckets either.
Reply
#3
I am not sure what you mean, but I guess you want a real copy and not two identifiers referencing the same object.
You can use the copy module for this.
>>> import copy
>>> dict1 = {"a": 1, "b":2}
>>> dict2 = {"y":25, "z": 26}
>>> dict1 = copy.copy(dict2)
>>> dict1
{'y': 25, 'z': 26}
>>> dict2["z"] = 33
>>> dict1
{'y': 25, 'z': 26}
Is this what you need?
Reply
#4
copy makes a new object. That would not work if you wanted to change an existing dictionary that might be referenced in several places. But referencing mutable objects is kind of dangerous anyway, especially if they might change without your knowledge. If I had a dictionary that changed, I would make it a property, and I would make a signal/event that you could use to be notified when the dictionary changed.
Reply
#5
the situation i i had previously used a list and had a limit to just one function call. other code had a reference to the same list, so replacing with a new one would not work. that rules out my 4th method. i have a variant case for this in which a dictionary would solve the variant case a dict can do almost everything for this. it just doesn't have a dict.replace() method i could use.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#6
Could you extend the dictionary to add a new method and use that instead?

class replacable_dict(dict):
    def replace(self, contents):
        self.clear()
        self.update(contents)

d = {"a":1, "b":2}

n = replacable_dict(c=3, d=4)
print(f"The variable currently holds {n}")
n.replace(d)
print(f"After replacement it holds {n}")
Output:
The variable currently holds {'c': 3, 'd': 4} After replacement it holds {'a': 1, 'b': 2}
Reply
#7
yeah, i could do that. even thought about it. but i was wondering if i had missed something. but, technically speaking, for list it's just doing slicing. it's not a.replace() method. so, why expect anything else to have it.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Sort a dict in dict cherry_cherry 4 75,811 Apr-08-2020, 12:25 PM
Last Post: perfringo

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020