When you use update like this, you are using keyword arguments, where you can pass parameters by name instead of by position in the list of arguments. The keyword in such arguments is treated as a name; the only way to have a variable keyword is to use the **kwargs syntax to pass an entire dict as a set of key=value parameters. That means that this would work . . .
update = { Var1: Var2 }
Dict.update(**update)
. . . except that keywords have to be strings, not numbers; trying to use your Var1 with value 1 as a keyword triggers a TypeError.
Because dict.update is designed for updating a dictionary, however, it also accepts an actual dictionary as an argument, without conversion to keyword arguments via **. So this does work:
Dict.update({ Var1: Var2 })
But you don't need update or its keyword arguments in your case; you can just use assignment instead:
Dict[Var1] = Var2
A couple style things: don't name variables with capital letters; that is usually an indication that a value is a constant. Also, don't use "Dict" as a variable name. Such names risk shadowing builtin functions, since functions and variables share the same namespace in Python. For example, if you assign a value to dict, you've just removed your pointer to the dict global function.
Python: Dictionary update using variables adds new keys but replaces old values with the new one - Stack Overflow
Updating a dictionary in python - Stack Overflow
How to update the value of a key in a dictionary in Python? - Stack Overflow
Here's a quick way to update a dictionary with a second dictionary: use the '|=' operator.
When you use update like this, you are using keyword arguments, where you can pass parameters by name instead of by position in the list of arguments. The keyword in such arguments is treated as a name; the only way to have a variable keyword is to use the **kwargs syntax to pass an entire dict as a set of key=value parameters. That means that this would work . . .
update = { Var1: Var2 }
Dict.update(**update)
. . . except that keywords have to be strings, not numbers; trying to use your Var1 with value 1 as a keyword triggers a TypeError.
Because dict.update is designed for updating a dictionary, however, it also accepts an actual dictionary as an argument, without conversion to keyword arguments via **. So this does work:
Dict.update({ Var1: Var2 })
But you don't need update or its keyword arguments in your case; you can just use assignment instead:
Dict[Var1] = Var2
A couple style things: don't name variables with capital letters; that is usually an indication that a value is a constant. Also, don't use "Dict" as a variable name. Such names risk shadowing builtin functions, since functions and variables share the same namespace in Python. For example, if you assign a value to dict, you've just removed your pointer to the dict global function.
Just this:
Dict[var1] = var2
Calling "update" is a different thing: it can update several keys at once, and it is more usually called with another dictionary passed as positional parameter. In the form .update(par_name=value) the left part is used by the language as a "named parameter" and it is fixed in code, as a literal.
Python has this feature built-in:
>>> d = {'b': 4}
>>> d.update({'a': 2})
>>> d
{'a': 2, 'b': 4}
Or given you're not allowed to use dict.update:
>>> d = dict(d.items() + {'a': 2}.items()) # doesn't work in python 3
With python 3.9 you can use an |= update operator:
>>> d = {'b': 4}
>>> d |= {'a': 2}
>>> d
{'a': 2, 'b': 4}
Well you could directly substract from the value by just referencing the key. Which in my opinion is simpler.
>>> books = {}
>>> books['book'] = 3
>>> books['book'] -= 1
>>> books
{'book': 2}
In your case:
book_shop[ch1] -= 1
d = {'A': 1, 'B': 5, 'C': 2}
d.update({'A': 2})
print(d)
{'A': 2, 'B': 5, 'C': 2}
for example, you have a dictionary 'DictA' and you want to update it with all the items from 'DictB'. Instead of looping on the keys, or items of DictB, you can simply:
DictA |= DictB
I use this A LOT, and I'm thankful to the Redditor who mentioned it in one of those long 'share your tips' threads...
1. You can update many keys on the same statement.
my_dict.update(other_dict)
In this case you don't have to know how many keys are in the other_dict. You'll just be sure that all of them will be updated on my_dict.
2. You can use any iterable of key/value pairs with dict.update
As per the documentation you can use another dictionary, kwargs, list of tuples, or even generators that yield tuples of len 2.
3. You can use the update method as an argument for functions that expect a function argument.
Example:
def update_value(key, value, update_function):
update_function([(key, value)])
update_value("k", 3, update_on_the_db) # suppose you have a update_on_the_db function
update_value("k", 3, my_dict.update) # this will update on the dict
d.update(n) is basically an efficient implementation of the loop
for key, value in n.items():
d[key] = value
But syntactically, it also lets you specify explicit key-value pairs without building a dict, either using keyword argumetns
d.update(a=1, b=2)
or an iterable of pairs:
d.update([('a', 1), ('b', 2)])