Multiply Python Dictionary Value by a Constant


The developers prefer Python language over other programming languages due to its simplicity and versatility. It contains various built-in data structures and one among them is the dictionary which can hold key pair elements. The elements inside the dictionary data structure are separated by a comma and provided inside the flower brackets that are “{}”. The dictionaries do not follow any orders of the elements and the key value assigned cannot be changed. The dictionary can hold the elements like lists, tuples, or other dictionaries.

Multiply Python Dictionary Value by a Constant

Dictionaries can be easily manipulated using various built-in functions provided by the language. One such useful function is to multiply the Python dictionary value by a constant. The basic syntax for the dictionary is, 

Dict_1 = {‘John’: ‘number’, ‘Bye’: 453}

There are many times when we need to adjust values present within Python Dictionaries, and multiplying the dictionary value by a constant is one such scenario where this feature can be very advantageous. Moreover, since Python offers multiple options to optimize memory utilization using built-in libraries such as Numpy or Panda and many more third-party libraries, we can perform data scaling effectively on much larger datasets with ease.

In conclusion, Python Dictionary is a great way of storing key-value pairs in programming language likewise one can easily scale the values by simply multiplying by a constant factor either through looping over items within that dictionary or exploiting advanced optimization techniques available in different scientific computation libraries compatible with Python.

Approach

  • Approach 1 − Using the iteration method

  • Approach 2 − Using the numpy module

Approach 1:  Python Program to multiply the dictionary value by a constant using the iteration method

The constant is multiplied with the dictionary values by simply using the for loop. This method is recommended when dealing with small dictionary sets, but as in the case of large data sets, it will not work out.

Algorithm

  • The dictionary set is initialized with values along with the key values.

  • Then given input is printed using the print function.

  • The scaling factor is defined according to user need and in this case, it’s 3.

  • The for loop will iterate through each element of the given dictionary and later multiply it with the constant

  • The output is returned as a dictionary set after multiplication

Example

# Initializing the dictionary data structure
dict1 = {'Tomato': 10, 'Carrot':20,'Beans':30}
print("Given Dictionary set is : ", dict1) 

# Adding a new key-value pair to the dictionary
dict1['radish'] = 40

# Multiplying the above dictionary with a constant value of 5
constant_value = 3

# Using get()
# Multiply Dictionary Value by Constant
for key in dict1:
    dict1[key] = dict1.get(key) * constant_value

# Final statement after multiplying with constant      
print("Dictionary value after multiplication is:",dict1)

Output

Given Dictionary set is :  {'Tomato': 10, 'Carrot': 20, 'Beans': 30}
Dictionary value after multiplication is: {'Tomato': 30, 'Carrot': 60, 'Beans': 90, 'radish': 120}

Approach 2:  Python Program to multiply the dictionary value by a constant using the numpy module

The numpy module is used to multiply the constant to the dictionary value by converting the given dictionary into numpy array and later performing the operations using the multiply function.

Algorithm

  • The numpy library is imported.

  • The dictionary is created with three values along with the key.

  • The constant that needs to be multiplied is initialized.

  • Numpy module will create a new dictionary by multiplying the same values but with a constant.

  • The print function returns the required output.

Example

#importing the module
import numpy as np

# Initializing the dictionary data structure
dict1 = {'Tomato': 10, 'Carrot':20,'Beans':30}
print("Given Dictionary set is : ", dict1) 

# Adding a new key-value pair to the dictionary
dict1['radish'] = 40

# Multiplying the above dictionary with a constant value of 3 using numpy
constant_value = 3

dict1 = {k: np.multiply(v, constant_value) for k, v in dict1.items()}

# Final statement after multiplying with constant      
print("Dictionary value after multiplication is:",dict1)

Output

Given Dictionary set is :  {'Tomato': 10, 'Carrot': 20, 'Beans': 30}
Dictionary value after multiplication is: {'Tomato': 30, 'Carrot': 60, 'Beans': 90, 'radish': 120}

Conclusion

Python Programming helps in reducing complex functionalities. The multiplications seem to be simple calculations, but in the case of large data sets, we need to use some in-built functions like numpy or pandas from the Python Language. When “for loop” is used to perform the above operations, it will lead to intensive memory and even crash the code.

Updated on: 01-Sep-2023

232 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements