Last updated: Apr 9, 2024
Reading timeยท5 min
To multiply the values in a dictionary by a constant:
dict.items()
method to get a view of the dictionary's items.dict.update()
method.import math my_dict = {'a': 2, 'b': 3, 'c': 4} # โ multiply dictionary values by number (in place) my_dict.update( (key, value * 2) for key, value in my_dict.items() ) # ๐๏ธ {'a': 4, 'b': 6, 'c': 8} print(my_dict)
The example multiplies the values of the dictionary by a constant, in place.
The dict.items() method returns a new view of the dictionary's items ((key, value) pairs).
my_dict = {'a': 2, 'b': 3, 'c': 4} # ๐๏ธ dict_items([('a', 2), ('b', 3), ('c', 4)]) print(my_dict.items())
We used a generator expression to iterate over the view of items.
On each iteration, we return a tuple containing 2 elements - a key and a value.
my_dict = {'a': 2, 'b': 3, 'c': 4} my_dict.update((key, value * 2) for key, value in my_dict.items()) # ๐๏ธ {'a': 4, 'b': 6, 'c': 8} print(my_dict)
The dict.update method updates the dictionary with the key/value pairs from the provided value.
The method overrides the dictionary's existing keys and returns None.
dict.update()
method can either be called with another dictionary or an iterable of key/value pairs (e.g. a list of tuples with 2 elements each).Alternatively, you can use a dict comprehension to get a new dictionary with the results of the multiplication.
my_dict = {'a': 2, 'b': 3, 'c': 4} new_dict = {key: value * 2 for key, value in my_dict.items()} # ๐๏ธ {'a': 4, 'b': 6, 'c': 8} print(new_dict)
Dict comprehensions are very similar to list comprehensions.
On each iteration, we multiply the current value by 2
and return the key-value
pair.
The new dictionary contains the multiplication results and the original dictionary remains unchanged.
You can also use a for loop to multiply a dictionary's values by a constant.
my_dict = {'a': 2, 'b': 3, 'c': 4} for key in my_dict: my_dict[key] *= 2 print(my_dict) # ๐๏ธ {'a': 4, 'b': 6, 'c': 8}
On each iteration, we set the current key to the result of multiplying its value
by 2
.
If you need to multiply all of the values in a dictionary, use the math.prod()
method.
import math my_dict = {'a': 2, 'b': 3, 'c': 4} # ๐๏ธ 24 (same as 2 * 3 * 4) print(math.prod(dict.values(my_dict)))
The dict.values method returns a new view of the dictionary's values.
my_dict = {'a': 2, 'b': 3, 'c': 4} print(my_dict.values()) # ๐๏ธ dict_values([2, 3, 4])
The math.prod() method calculates the product of all the elements in the provided iterable.
import math my_tuple = (5, 5, 5) result = math.prod(my_tuple) print(result) # ๐๏ธ 125
The method takes the following 2 arguments:
Name | Description |
---|---|
iterable | An iterable whose elements to calculate the product of |
start | The start value for the product (defaults to 1 ) |
If the iterable is empty, the start
value is returned.
for
loopYou can also use a for
loop to multiply all of the values in a dictionary.
my_dict = {'a': 2, 'b': 3, 'c': 4} result = 1 for value in my_dict.values(): result = result * value print(result) # ๐๏ธ 24
The code sample uses a for
loop to iterate over the dictionary's values.
On each iteration, we multiply the current value by the result
variable and
update its value.
If you need to multiply two dictionaries, use a dict
comprehension.
dict_1 = { 'a': 2, 'b': 3, 'c': 4, } dict_2 = { 'a': 3, 'b': 4, 'c': 5 } result = {key: dict_1[key] * dict_2[key] for key in dict_1} # ๐๏ธ {'a': 6, 'b': 12, 'c': 20} print(result) print(sum(result.values())) # ๐๏ธ 38
We used a dict comprehension to iterate over one of the dictionaries.
Dict comprehensions are very similar to list comprehensions.
On each iteration, we access the same key from the two dictionaries and multiply the values.
dict.items()
Alternatively, you can use the dict.items()
method to get a view of the
dictionary's items.
dict_1 = { 'a': 2, 'b': 3, 'c': 4, } dict_2 = { 'a': 3, 'b': 4, 'c': 5 } # ๐๏ธ dict_items([('a', 2), ('b', 3), ('c', 4)]) print(dict_1.items()) result = {key: num * dict_2[key] for key, num in dict_1.items()} # ๐๏ธ {'a': 6, 'b': 12, 'c': 20} print(result)
The dict.items method returns a new view of the dictionary's items ((key, value) pairs).
If you need to get the sum of the values in the new dictionary, pass the result
of calling dict.values()
to the sum()
function.
dict_1 = { 'a': 2, 'b': 3, 'c': 4, } dict_2 = { 'a': 3, 'b': 4, 'c': 5 } # ๐๏ธ dict_items([('a', 2), ('b', 3), ('c', 4)]) print(dict_1.items()) result = {key: num * dict_2[key] for key, num in dict_1.items()} # ๐๏ธ {'a': 6, 'b': 12, 'c': 20} print(result) print(result.values()) # ๐๏ธ dict_values([6, 12, 20]) print(sum(result.values())) # ๐๏ธ 38
The dict.values() method returns a new view of the dictionary's values.
The sum() function takes an iterable, sums its items from left to right and returns the total.
You can learn more about the related topics by checking out the following tutorials: