Multiplying Dictionary Values

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

Example 1:

Input: {"a": 5, "b": 3, "c": 2} 
Output: 30

Example 2:

Input: {"x": 7, "y": 2, "z": 1} 
Output: 14

You can use Python’s built-in reduce() function.

from functools import reduce
def multiply_dict(d):
    return reduce((lambda x, y: x * y), d.values())

© Let’s Data Science

LOGIN

Unlock AI & Data Science treasures. Log in!