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
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.
>>> 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}
Here's a quick way to update a dictionary with a second dictionary: use the '|=' operator.
When updating a dictionary using a for loop, all keys return the same value (despite iterating through multiple values.)
Can a dictionary automatically update another dictionary with a shared key?
Update values in a dictionary, from a list, if the value is currently 1
Videos
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...
I'll give more context about the specific problem down below but first to make it simple:
I have 2 dictionaries that share 1 key. The program is going to take an input from the user, compare it to a list of possible inputs, and change the value of that shared key depending on what input was given.
The way its written now, the input only changes the value on one dictionary, not both. So how do I make it so changing the value of a key on one dictionary makes it pass the new value onto every other dictionary that uses the same key?
For example:
dic_1 = {'a': 7, 'b': 19, 'c': ' '}
dic_2 = {'c': ' ', 'd': 34, 'e': 35}
some_list = [pretend this is a list of values]
def some_function():
var = input()
if var in some_list:
dic_1['c'] = 17To sum it up, the user inputs a value that is referenced to a list, and depending on what value in the list was input, the value of key['c'] is changed in dic_1.
How do I make this change also automatically change the value of dic_2, and any other dictionary that also shares the key['c']?
Am I going to have to manually include it in some_function()? Or how would the best way to do it?
Tl;Dr Multiple dictionaries share the same key. How do I make it so changing the value of that key in one dictionary also changes the value of the key in every dictionary?
I'm probably way over-engineering this but hey I'm a really rusty and learning it all again after over a year of not touching a single programming language
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
I have been trying to do this singular issue for a couple hours. I basically want to get the value of one key in my dict and add to it, but not to any of the other keys. I have searched all over the internet and can not find or just do not know how to word it to find a solution. This is what I have soo far, which creates the dictionary with user inputs and assigns 1 to all their values. Any help would save my mental state.
EDIT: wow extremely helpful community here. I'm actually really impressed. Will take all the info here and apply it. Saved me hours of frustration.
list = {}
while True:
try:
item = input()
value = 1
list[item] = value
sorted_list = dict(sorted(list.items()))
except EOFError:
break
for item in sorted_list:
print(str(value) + " " + item.upper())