Multiply dictionary values by a constant in Python

avatar

Borislav Hadzhiev

Last updated: Jul 9, 2022

banner

Photo from Unsplash

Multiply dictionary values by a constant in Python #

To multiply the values in a dictionary by a constant:

  1. Use the dict.items() method to get a view of the dictionary's items.
  2. Use a generator expression to iterate over the view and return tuples of key-value pairs.
  3. Pass the result to the dict.update() method.
main.py
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) # ------------------------------------------------ # ✅ multiply dictionary values by number (without mutation) my_dict_2 = {'a': 2, 'b': 3, 'c': 4} result = {key: value * 2 for key, value in my_dict_2.items()} # 👇️ {'a': 4, 'b': 6, 'c': 8} print(result) # ------------------------------------------------ # ✅ multiply all of the values in a dictionary my_dict = {'a': 2, 'b': 3, 'c': 4} print(math.prod(dict.values(my_dict))) # 👉️ 24 (same as 2 * 3 * 4)

The first 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).

main.py
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.

Generator expressions are used to perform some operation for every element, or select a subset of elements that meet a condition.

On each iteration, we return a tuple containing 2 elements - a key and a value.

main.py
my_dict = {'a': 2, 'b': 3, 'c': 4} # 👇️ dict_items([('a', 2), ('b', 3), ('c', 4)]) print(my_dict.items()) 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.

The 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.

main.py
my_dict = {'a': 2, 'b': 3, 'c': 4} result = {key: value * 2 for key, value in my_dict.items()} # 👇️ {'a': 4, 'b': 6, 'c': 8} print(result)

Dict comprehensions are very similar to list comprehensions.

They perform some operation for every key-value pair in the dictionary, or select a subset of key-value pairs that meet a condition.

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.

If you need to multiply all of the values in a dictionary, use the math.prod() method.

main.py
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.

main.py
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.

main.py
import math my_tuple = (5, 5, 5) result = math.prod(my_tuple) print(result) # 👉️ 125

The method takes the following 2 arguments:

NameDescription
iterableAn iterable whose elements to calculate the product of
startThe start value for the product (defaults to 1)

If the iterable is empty, the start value is returned.

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.