Skip to main content

How to Multiply Dictionary Values in Python

This guide explores various techniques for multiplying the values in a Python dictionary by a constant and for multiplying the values in one dictionary by the corresponding values in another dictionary. We'll cover in-place modification and creating new dictionaries, using dict comprehensions and loops.

Multiplying Dictionary Values by a Constant

You can multiply each value in a dictionary by a constant using several approaches.

In-Place Modification with dict.update()

The dict.update() method can modify the dictionary in-place, avoiding the creation of a new dictionary object.

my_dict = {'a': 2, 'b': 3, 'c': 4}
my_dict.update((key, value * 2) for key, value in my_dict.items())
print(my_dict) # Output: {'a': 4, 'b': 6, 'c': 8}
  • The update() method is called on the original dictionary to modify the values of every key-value pair.
  • The code uses a generator expression (key, value * 2) for key, value in my_dict.items() which iterates through every key-value pair to create tuples of key value pairs, where the value is two times the initial value.

Creating a New Dictionary with a Dict Comprehension

A dict comprehension provides a concise way to create a new dictionary with the multiplied values, leaving the original dictionary unchanged:

my_dict = {'a': 2, 'b': 3, 'c': 4}
new_dict = {key: value * 2 for key, value in my_dict.items()}
print(new_dict) # Output: {'a': 4, 'b': 6, 'c': 8}
  • new_dict will be a new dictionary containing the key-value pairs of the first dictionary, with the values updated.
  • my_dict remains untouched.

In-Place Multiplication with a for Loop

You can also use a for loop to iterate over the items and update the dictionary:

my_dict = {'a': 2, 'b': 3, 'c': 4}

for key in my_dict:
my_dict[key] *= 2

print(my_dict) # Output: {'a': 4, 'b': 6, 'c': 8}
  • The for loop iterates over the dictionary's keys using for key in my_dict.
  • The *= 2 operator multiplies the value for every key by 2 in place.

Multiplying Two Dictionaries

To multiply the values in one dictionary by the corresponding values in another dictionary (assuming shared keys), you can use a dict comprehension. This technique creates a new dictionary:

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}
print(result) # Output: {'a': 6, 'b': 12, 'c': 20}
  • We iterate through the keys from one of the dictionaries to perform the multiplication using the corresponding values from both dictionaries. This approach will throw an exception if a key exists in dict_1 but not in dict_2.

Summing the Values of the Resulting Dictionary

To calculate the sum of the values in the resulting 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}

result = {key: dict_1[key] * dict_2[key] for key in dict_1}
print(result.values()) # Output: dict_values([6, 12, 20])
print(sum(result.values())) # Output: 38