Looking to gain some understanding on handling dictionaries efficiently in Python.
I have a dictionary
dict = {'a': 11, 'b': 4, 'c': 7, 'd': 12, 'e': 5}
and I would like to perform calculations only on a subset of its values. Which values to be considered in the calculations is based on their keys. Say, that these keys will be in a list of keys.
For example, if I would like to add the values of the keys a
, b
and e
(i.e. keys_list_to_add
=['a', 'b', 'e']) I would expect the result to be 20
. Note, that several such subsets calculations may exist and a key may be missing (so, perhaps an exception should be raised in that case).
After seeing this answer, I tried to implement it with no success cause I am still learning Python. Could you please provide with code suggestions for my problem?