{"id":16542,"date":"2021-08-12T09:24:19","date_gmt":"2021-08-12T03:54:19","guid":{"rendered":"https:\/\/python-programs.com\/?p=16542"},"modified":"2021-11-22T18:38:33","modified_gmt":"2021-11-22T13:08:33","slug":"python-program-to-find-product-of-values-of-elements-in-a-dictionary","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-program-to-find-product-of-values-of-elements-in-a-dictionary\/","title":{"rendered":"Python Program to Find Product of Values of elements in a Dictionary"},"content":{"rendered":"
In the previous article, we have discussed Python Program to Check if a String is a keyword or Not<\/a> A dictionary is a set of elements that have key-value pairs. The values in the elements are accessed using the element’s keys.<\/p>\n example:<\/p>\n dict = {‘january’ :1, ‘febrauary’: 2, ‘march’: 3 }<\/p>\n Given a dictionary, and the task is to find the Product of values of elements in a dictionary.<\/p>\n Examples:<\/strong><\/p>\n Example1:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n Example2:<\/strong><\/p>\n Input:\u00a0<\/strong><\/p>\n Output:<\/strong><\/p>\n Below are the ways to Find the Product of Values of elements in a Dictionary.<\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Explore more instances related to python concepts from\u00a0Python Programming Examples<\/a>\u00a0Guide and get promoted from beginner to professional programmer level in Python Programming Language.<\/p>\n <\/p>\n","protected":false},"excerpt":{"rendered":" In the previous article, we have discussed Python Program to Check if a String is a keyword or Not Dictionary in python : A dictionary is a set of elements that have key-value pairs. The values in the elements are accessed using the element’s keys. example: dict = {‘january’ :1, ‘febrauary’: 2, ‘march’: 3 } …<\/p>\n
\nDictionary in python :<\/strong><\/p>\nGiven dictionary = {'jan': 10, 'Feb': 5, 'Mar': 22, 'April': 32, 'May': 6}<\/pre>\n
The Product of values in a given dictionary = 211200<\/pre>\n
Given dictionary = {'a': 1, 'b': 5, 'c': 2, 'd': 4, 'e': 7, 'f': 2}<\/pre>\n
The Product of values in a given dictionary = 560<\/pre>\n
Program to Find Product of Values of elements in a Dictionary<\/h2>\n
\n
Method #1: Using For Loop (Static Input)<\/h3>\n
\n
# Give the dictionary as static input and store it in a variable.\r\ngvn_dict = {'jan': 10, 'Feb': 5, 'Mar': 22, 'April': 32, 'May': 6}\r\n# Get all the values of given dictionary using dictionary.values() method\r\n# and store it in another variable.\r\ndict_vlue = gvn_dict.values()\r\n# Take a variable say 'product' and initialize it's value with '1'\r\nfnl_prod = 1\r\n# Iterate in the above given dictionary values using using For loop.\r\nfor itrator in dict_vlue:\r\n # Inside the loop, Multiply the above initialized product variable with the iterator\r\n # and store it in a same variable.\r\n fnl_prod = fnl_prod*itrator\r\n# Print the product of values for the above given dictionary.\r\nprint(\"The Product of values in a given dictionary = \", fnl_prod)\r\n<\/pre>\n
The Product of values in a given dictionary = 211200<\/pre>\n
Method #2: Using For Loop (User Input)<\/h3>\n
\n
# Take a dictionary and initialize it with an empty dictionary using dict() or {}.\r\ngvn_dict = {}\r\n# Give the number of keys as user input using int(input()) and store it in a variable.\r\nnumb_of_kys = int(\r\n input('Enter some random number of keys of the dictionary = '))\r\n# Loop till the given number of keys using for loop.\r\nfor p in range(numb_of_kys):\r\n # Inside the for loop scan the key and value as\r\n # user input using input(),split() functions\r\n # and store them in two separate variables.\r\n keyy, valuee = input(\r\n 'Enter key and value separated by spaces = ').split()\r\n # Initialize the key with the value of the dictionary.\r\n gvn_dict[keyy] = valuee\r\n\r\n# Get all the values of given dictionary using dictionary.values() method\r\n# and store it in another variable.\r\ndict_vlue = gvn_dict.values()\r\n# Take a variable say 'product' and initialize it's value with '1'\r\nfnl_prod = 1\r\n# Iterate in the above given dictionary values using using For loop.\r\nfor itrator in dict_vlue:\r\n # Inside the loop, Multiply the above initialized product variable with the iterator\r\n # and store it in a same variable.\r\n fnl_prod = fnl_prod*int(itrator)\r\n# Print the product of values for the above given dictionary.\r\nprint(\"The Product of values in a given dictionary = \", fnl_prod)\r\n<\/pre>\n
Enter some random number of keys of the dictionary = 5\r\nEnter key and value separated by spaces = hello 4\r\nEnter key and value separated by spaces = this 9\r\nEnter key and value separated by spaces = is 10\r\nEnter key and value separated by spaces = btechgeeks 12\r\nEnter key and value separated by spaces = python 1\r\nThe Product of values in a given dictionary = 4320<\/pre>\n
\n