Python Program to Multiply All the Items in a Dictionary

PythonServer Side ProgrammingProgramming

When it is required to multiply all the elements in a dictionary, the key values in the dictionary are iterated over. The key is multiplied with the previous key, and output is determined.

The dictionary is a set of key-value pairs.

Example

Below is a demonstration for the same −

 Live Demo

my_dict = {'Jane':99,'Will':54,'Mark':-3}
my_result = 2
for key in my_dict:
   my_result = my_result * my_dict[key]
print("The reuslt of multiplying keys in a dictionary is : ")
print(my_result)

Output

The reuslt of multiplying keys in a dictionary is :
-32076

Explanation

  • A dictionary is defined.
  • A variable is assigned a certain value.
  • The ‘key’ in the dictionary is iterated over.
  • In every step, this key is multiplied with the variable previously assigned.
  • This value is assigned to the same variable itself.
  • This is displayed on the console as the output.
raja
Published on 12-Mar-2021 12:48:13
Advertisements