Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- new = ['coin', 'coin', 'apple', 'axe', 'sword']
- items = {'coin': 43, 'apple': 5}
- for x in new:
- if x in items:
- items[x] += 1
- else:
- items[x] = 0
- print(items)
- #{'sword': 0, 'coin': 45, 'apple': 6, 'axe': 0}
- items = {'coin': 43, 'apple': 5}
- new = ['coin', 'coin', 'apple', 'axe', 'sword']
- from collections import Counter
- def add_to_dict(x: dict, y: list):
- for k, v in Counter(y).items():
- try:
- x[k] += v
- except KeyError:
- x[k] = v
- add_to_dict(items, new)
- print(items)
Add Comment
Please, Sign In to add comment