Blessing988

Untitled

Mar 8th, 2020
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.48 KB | None | 0 0
  1.  
  2. #my function
  3.  
  4. def sum_of_values(my_dict, a_list):
  5.  
  6.     total = 0
  7.  
  8.     for key in my_dict:
  9.  
  10.         if key in a_list:      #if the key in the dictionary is also in the list
  11.  
  12.             total += my_dict[key]  #this will keep on adding the target corresponding value of the key
  13.  
  14.     print(total)
  15.  
  16.  
  17. my_dict = {"a": 5, "b": 3, "c": 10}  #sample dictionary
  18.  
  19.  
  20. sum_of_values(my_dict, ["a", "c"])            #Example
  21. sum_of_values(my_dict, ["a", "b", "c"])
Add Comment
Please, Sign In to add comment