Summing Dictionary Values

Write a Python program to sum all the items in a dictionary.

Example 1:

Input: {"a": 5, "b": 3, "c": 8} 
Output: 16

Example 2:

Input: {"x": 7, "y": 6, "z": 9} 
Output: 22

You can use Python’s built-in sum() function and dict.values() method.

def sum_dict(d):
    return sum(d.values())

© Let’s Data Science


Unlock AI & Data Science treasures. Log in!