[Solved] How to add the value of keys in an array to a dictionary? [closed]


To sum the values, you can loop over the data using .items(), will return the key with its associated value at each iteration:

s = {'A': [3, 4, 7], 'B': [4, 9, 9], 'C': [3, 4, 5], 'D': [2, 2, 6], 'E': [6, 7, 9], 'F': [2, 4, 5]}
new_dict = {}
for a, b in s.items():
   new_dict[a] = sum(b) #here, using sum function to get the total of all elements

Dictionaries are unsorted, however, you can keep the data in this format:

final_data = sorted(new_dict.items(), key=lambda x:x[-1])
final_output = final_data[2:]
print(final_output)

12

solved How to add the value of keys in an array to a dictionary? [closed]