Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Python Compute the Sum of Dictionary Values

Python Compute the Sum of Dictionary Values

Document Object Reference Manual Python3 Examples

Given a dictionary, compute the sum of all its numeric values.

Example

def

returnSum

(

myDict

)

:

sum

=

0

for

i

in

myDict

:

sum

=

sum

myDict

[

i

]

return

sum

dict

= {

'

a

'

:

100

,

'

b

'

:

200

,

'

c

'

:

300

}

print

(

"

Sum :

"

,

returnSum

(

dict

)

)

The output of the code above is:


Sum : 600

Document Object Reference Manual Python3 Examples