Write a Python program to multiply all the items in a dictionary | Code

  • by

Use for loop and variable to Write a Python program to multiply all the items in a dictionary.

Python program to multiply all the items in a dictionary

Simple example code multiplies all the items in a dictionary. Run a loop to traverse through the dictionary and multiply each value of key with the result and add to the result itself.

my_dict = {'data1': 1, 'data2': -2, 'data3': 3}
res = 1

for key in my_dict:
    res = res * my_dict[key]

print(res)

Output:

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

Another example

d = {
    'a': 10,
    'b': 70,
    'c': 20,
}

# create a variable to store result
answer = 1

for i in d:
    answer = answer * d[i]

print(answer)

Output: 14000

Do comment if you have any doubts or suggestions on this Python multipicaiton topic.

Note: IDE: PyCharm 2021.3 (Community Edition)

Windows 10

Python 3.10.1

All Python Examples are in Python 3, so Maybe its different from python 2 or upgraded versions.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.