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
Answer from Christian W. on Stack OverflowWell 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}
Update values in a dictionary, from a list, if the value is currently 1
Need to modify a value in a dictionary to a float and append dictionary
Update Dictionary Value if Key exists
How to change a dicts value for one of the keys?
Videos
Say I have code like so:
my_dict = {date1:0, date2:1, date3:0, date4:1} my_list = [80, 20]
Is there a way to get output as follows:
new_dict = {date1:0, date2:80, date3:0 date4:20}
I've tried so much at this point but just cannot get it working, is my initial structure the issue? i.e. you're not supposed to use dictionaries like this? Either you can't do this, or there's a fundamental gap in my knowledge preventing me connecting the dots. Or I'm dumb!
TIA