Open In App

Python program to multiply all the items in a dictionary

Last Updated : 17 Apr, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Python program to illustrate multiplying all the items in a dictionary could be done by creating a dictionary that will store all the key-value pairs, multiplying the value of all the keys, and storing it in a variable.

Example:

Input: dict = {‘value1’:5, ‘value2’:4, ‘value3’:3, ‘value4’:2, ‘value5’:1} 
Output: ans = 120 

Input: dict = {‘v1’:10, ‘v2’:7, ‘v3’:2} 
Output: ans = 140

Approach 1:

  • Create a dictionary d and store key-value pairs in it.
  • Create a variable answer initialized to 1.
  • Run a loop to traverse through the dictionary d
  • Multiply each value of key with answer and store the result in answer itself.
  • Print answer.

Below are the examples of above approach.

Example 1:

Python3




# create a dictionary
d = {
    'value1': 5,
    'value2': 4,
    'value3': 3,
    'value4': 2,
    'value5': 1,
}
 
# create a variable to store result
answer = 1
 
# run a loop
for i in d:
    answer = answer*d[i]
 
# print answer
print(answer)


Output

120

Time complexity: O(n), where n is the number of elements in the dictionary.
Auxiliary space: O(1), as only a single variable is used to store the result.

Example 2:

Python3




# create a dictionary
d = {
    'a': 10,
    'b': 7,
    'c': 2,
}
 
# create a variable to store result
answer = 1
 
# run a loop
for i in d:
    answer = answer*d[i]
 
# print answer
print(answer)


Output

140

Approach 2: Using values() and list() methods

Python3




# create a dictionary
d = {
    'value1': 5,
    'value2': 4,
    'value3': 3,
    'value4': 2,
    'value5': 1,
}
# create a variable to store result
answer = 1
val=list(d.values())
 
# run a loop
for i in val:
    answer = answer*i
# print answer
print(answer)


Output

120

Time Complexity: O(n), where n is the number of key-value pairs in the dictionary. 

Auxiliary Space Complexity: O(1), as the amount of additional space used is constant regardless of the size of the input. 

Approach 3: Using functools.reduce(), operator.mul

Python3




# create a dictionary
d = {
    'value1': 5,
    'value2': 4,
    'value3': 3,
    'value4': 2,
    'value5': 1,
}
# create a variable to store result
answer = 1
val=list(d.values())
from functools import reduce
import operator
 
# print answer
print(reduce(operator.mul,val, 1))


Output

120

Time complexity: O(n), where n is the number of key-value pairs in the dictionary.

Auxiliary space: O(n), where n is the number of key-value pairs in the dictionary. 



Similar Reads

Python - How to Multiply all items in Tuple
Sometimes, while programming, we have a problem in which we might need to perform product among tuple elements. This is an essential utility as we come across product operation many times and tuples are immutable and hence required to be dealt with. Let’s discuss certain ways in which this task can be performed. Method #1 : Using list() + loop The
6 min read
Python Program to find the profit or loss when CP of N items is equal to SP of M items
Given [Tex]N   [/Tex]and [Tex]M   [/Tex]denoting that the Cost Price of N articles is equal to the Selling Price of M articles. The task is to determine the profit or Loss percentage. Examples:  Input: N = 8, M = 9 Output: Loss = -11.11% Input: N = 8, M = 5 Output: Profit = 60% Formula:-  Below is the implementation of the above approach: C/C++ Cod
1 min read
Python program to find the sum of all items in a dictionary
Given a dictionary in Python, write a Python program to find the sum of all items in the dictionary. Examples: Input : {'a': 100, 'b':200, 'c':300}Output : 600 Input : {'x': 25, 'y':18, 'z':45}Output : 88 Method #1: Using Inbuilt sum() Function Use the sum function to find the sum of dictionary values. C/C++ Code # Python3 Program to find sum of #
6 min read
Python - All possible items combination dictionary
Sometimes, while working with data, we can have a problem in which we are provided with sample data of key and value list and we require to construct actual data as all possible combination of keys and value list of similar size. Lets discuss certain ways in which this task can be performed. Method #1 : Using loop + set() The combination of above f
4 min read
Python | Multiply Dictionary Value by Constant
Sometimes, while working with dictionaries, we can have a use-case in which we require to multiply a particular key’s value by K in dictionary. It may seem a quite straight forward problem, but catch comes when the existence of a key is not known, hence becomes a 2 step process at times. Let’s discuss certain ways in which this task can be performe
3 min read
Python program to convert URL Parameters to Dictionary items
Given URL Parameters string, convert to dictionary items. Input : test_str = 'gfg=4&is=5' Output : {'gfg': ['4'], 'is': ['5']} Explanation : gfg's value is 4. Input : test_str = 'gfg=4' Output : {'gfg': ['4']} Explanation : gfg's value is 4 as param. Method #1 : Using urllib.parse.parse_qs() This is default inbuilt function which performs this
5 min read
Python program to extract dictionary items for custom values
Given a dictionary, extract all the items that match given values in the list Example: Input : test_dict = {"Gfg" : 3, "is" : 5, "for" : 8, "Geeks" : 10, "Best" : 16 }, sub_list = [5, 4, 10, 20, 16, 8] Output : {'is': 5, 'Geeks': 10, 'Best': 16, "for" : 8} Explanation : All values matching list values extracted along with keys. Input : test_dict =
4 min read
Python Program to create a sub-dictionary containing all keys from dictionary list
Given the dictionary list, our task is to create a new dictionary list that contains all the keys, if not, then assign None to the key and persist of each dictionary. Example: Input : test_list = [{'gfg' : 3, 'is' : 7}, {'gfg' : 3, 'is' : 1, 'best' : 5}, {'gfg' : 8}]Output : [{'is': 7, 'best': None, 'gfg': 3}, {'is': 1, 'best': 5, 'gfg': 3}, {'is':
8 min read
Python Dictionary items() method
Dictionary in Python is an unordered collection of data values, used to store data values like a map, which unlike other Data Types that hold only single value as an element, Dictionary holds key : value pair.In Python Dictionary, items() method is used to return the list with all dictionary keys with values. Syntax: dictionary.items()Parameters: T
2 min read
Python | Delete items from dictionary while iterating
A dictionary in Python is an ordered collection of data values. Unlike other Data Types that hold only a single value as an element, a dictionary holds the key: value pairs. Dictionary keys must be unique and must be of an immutable data type such as a: string, integer or tuple. Note: In Python 2 dictionary keys were unordered. As of Python 3, they
3 min read
Practice Tags :