208,346 views
16 votes
16 votes
Define a function named sum_values with two parameters. The first parameter will be a list of dictionaries (the data). The second parameter will be a string (a key). Return the sum of the dictionaries' values associated with the second parameter. You SHOULD NOT assume that all of the dictionaries in the first parameter will have the second parameter as a key. If none of the dictionaries have the second parameter as a key, your function should return 0. Sample function call: sum_values(data, 'estimated_annual_kwh_savings') sum_values_by_year

User Taha EL BOUFFI
by
2.3k points

1 Answer

26 votes
26 votes

Answer:

Answered below

Step-by-step explanation:

//Program is written in Python

sum = 0

def sum_of_values(dict_data, number_of_boys):

for dict in dict_data:

for key in dict.keys():

if key == number_of_boys:

sum += dict[key]

//After looping check the sum variable and //return the appropriate value.

if sum != 0:

return sum

elif sum == 0:

//There was no key of such so no addition.

return 0

User Unistudent
by
2.8k points