KnowledgeBoat Logo

Computer Science

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

Python Dictionaries

1 Like

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())

Output

6

Answered By

2 Likes


Related Questions