CBSE Class 11 Computer Science Question 75 of 112

Dictionaries — Question 15

Back to all questions
15
Question

Question 15

Can you use sum( ) for calculating the sum of the values of a dictionary ?

Answer

It is not possible to use the sum( ) function to calculate the sum of values in a dictionary, as sum( ) function only works with the keys, and only when the keys are homogenous and addition compatible.

We can calculate the sum of the values of a dictionary with the help of values( ) and sum( ) functions as shown in the example below:

d = {"abc" : 1 ,"def" : 2 , "mno" : 3}
sum(d.values())