Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #my function
- def sum_of_values(my_dict, a_list):
- total = 0
- for key in my_dict:
- if key in a_list: #if the key in the dictionary is also in the list
- total += my_dict[key] #this will keep on adding the target corresponding value of the key
- print(total)
- my_dict = {"a": 5, "b": 3, "c": 10} #sample dictionary
- sum_of_values(my_dict, ["a", "c"]) #Example
- sum_of_values(my_dict, ["a", "b", "c"])
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement