I think the most effective way to do it would be something like this:
for k, v in myDict2.iteritems():
myDict1[k] = myDict1.get(k, ()) + v
But there isn't an update equivalent for what you're looking to do, unfortunately.
performance - Updating a python dictionary while adding to existing keys? - Stack Overflow
Update python dictionary (add another value to existing key) - Stack Overflow
Equivalent of dictionary.update() but for appending when value is a collection?
How to update the value of a key in a dictionary in Python? - Stack Overflow
Videos
I think the most effective way to do it would be something like this:
for k, v in myDict2.iteritems():
myDict1[k] = myDict1.get(k, ()) + v
But there isn't an update equivalent for what you're looking to do, unfortunately.
The fastest way to merge large dictionaries is to introduce an intermediate object that behaves as though the dicts are merged without actually merging them (see @Raymond Hettinger's answer):
from collections import ChainMap
class MergedMap(ChainMap):
def __getitem__(self, key):
result = []
found = False
for mapping in self.maps:
try:
result.extend(mapping[key])
found = True
except KeyError:
pass
return result if found else self.__missing__(key)
merged = MergedMap(myDict1, myDict2)
Whether it is applicable depends on how you want to use the combined dict later.
It uses collections.ChainMap from Python 3.3+ for convenience to provide the full MutableMapping interface; you could implement only parts that you use on older Python versions.
Well you can simply use:
d['word'] = [1,'something']
Or in case the 1 needs to be fetched:
d['word'] = [d['word'],'something']
Finally say you want to update a sequence of keys with new values, like:
to_add = {'word': 'something', 'word1': 'something1'}
you could use:
for key,val in to_add.items():
if key in d:
d[key] = [d[key],val]
You could write a function to do this for you:
>>> d = {'word': 1, 'word1': 2}
>>> def set_key(dictionary, key, value):
... if key not in dictionary:
... dictionary[key] = value
... elif type(dictionary[key]) == list:
... dictionary[key].append(value)
... else:
... dictionary[key] = [dictionary[key], value]
...
>>> set_key(d, 'word', 2)
>>> set_key(d, 'word', 3)
>>> d
{'word1': 2, 'word': [1, 2, 3]}
Alternatively, as @Dan pointed out, you can use a list to save the data initially. A Pythonic way if doing this is you can define a custom defaultdict which would add the data to a list directly:
>>> from collections import defaultdict
>>> d = defaultdict(list)
>>> d[1].append(2)
>>> d[2].append(2)
>>> d[2].append(3)
>>> d
defaultdict(<type 'list'>, {1: [2], 2: [2, 3]})
I just learned about the update method while doing some work. I had been writing if statements to check if key is in .keys() for so long. Is there an equivalent for "add key: value if not already in dictionary, otherwise update the value"?
For example to replace:
if ind in collection.keys(): collection[ind].append(x) else: collection[ind] = [x]
Well you could directly substract from the value by just referencing the key. Which in my opinion is simpler.
Copy>>> books = {}
>>> books['book'] = 3
>>> books['book'] -= 1
>>> books
{'book': 2}
In your case:
Copybook_shop[ch1] -= 1
Copyd = {'A': 1, 'B': 5, 'C': 2}
d.update({'A': 2})
print(d)
Copy{'A': 2, 'B': 5, 'C': 2}