How do you multiply all values of an array with a constant in python?

Posted on: March 12, 2021 by Deven


In this article, you will learn how to multiply array by scalar in python.

Let’s say you have 2 arrays that need to be multiplied by scalar n .

array1 = np.array[[1, 2, 3]]
array2 = np.array[[[1, 2], [3, 4]]]
n = 5

Numpy multiply array by scalar

In order to multiply array by scalar in python, you can use np.multiply[] method.

import numpy as np
array1 = np.array[[1, 2, 3]]
array2 = np.array[[[1, 2], [3, 4]]]
n = 5
np.multiply[array1,n]
np.multiply[array2,n]

Share on social media

//

PreviousNext

You can multiply numpy arrays by scalars and it just works.

>>> import numpy as np
>>> np.array[[1, 2, 3]] * 2
array[[2, 4, 6]]
>>> np.array[[[1, 2, 3], [4, 5, 6]]] * 2
array[[[ 2,  4,  6],
       [ 8, 10, 12]]]

This is also a very fast and efficient operation. With your example:

>>> a_1 = np.array[[1.0, 2.0, 3.0]]
>>> a_2 = np.array[[[1., 2.], [3., 4.]]]
>>> b = 2.0
>>> a_1 * b
array[[2., 4., 6.]]
>>> a_2 * b
array[[[2., 4.],
       [6., 8.]]]

Python is the best programming language for many developers. It is so easy that anyone can start learning without any previous programming experience. In this entire tutorial, you will learn how to multiply all elements in a list by constant in python using various methods.

In this section, you will know all the best methods to multiply all elements in the list by constant using python language. Let’s get started.

Method 1: Multiplying elements using list comprehension

The first method to multiply all elements in the list by constant in python is List comprehension. List comprehension is used to generate a new list by iterating on the existing lists, tuples, strings, arrays e.t.c. The syntax for the list comprehension is below.

finalList = [ expression[i] for i in existingList if condition ]

Run the below lines of code to multiply all the elements in the list by some constant.

sample_list = [100, 200, 300, 400, 500]
new_list = [i * 10 for i in sample_list]
print[new_list]

You can see I have multiplied all the elements of the list by 10by using the expression i * 10. Here ” i “  denotes each element of the list.

Output

Multiplying all elements of the list by constant in python using the list comprehension

Method 2: Multiply all elements in the list by constant in Python using for loop

The above method was using the list comprehension that is indirectly a loop. In this method, you will use it directly for  loop. It will iterate each element and multiply each item by the constant. In addition, it will append each multiplied result to the new list.

Execute the below lines of code to multiply all elements in the list by constant in python.

sample_list = [100, 200, 300, 400, 500]
new_list = []
for i in sample_list:
    new_list.append[i * 10]
print[new_list]

Output

Multiplying all elements of the list by constant in python using the for loop

Method 3: Multiplying using pandas

The third method to multiply elements in the list is through the pandas Python package. Pandas allow you to convert any list or other data structures into Dataframe and Series. In this example, you will first convert a list to a Series and multiply it by constant. After that, you will convert the Series to list by tolist[]method.

Run the below lines of code to multiply each element.

import pandas as pd
sample_list = [100, 200, 300, 400, 500]
series = pd.Series[sample_list]
mul_series = series*10
print[mul_series.tolist[]]

Output

Multiplying all elements of the list by constant using pandas

Method 4: Multiplying using NumPy

The above method was using the pandas package. The other method to multiply the elements of the list is using the NumPy python packages. You have to first convert the list to NumPy array and then multiply it by the constant. After that, you will convert the NumPy array to a list using the tolist[] method.

import numpy as np
sample_list = [100, 200, 300, 400, 500]
numpy_array= np.array[sample_list]
mul_array = numpy_array * 10
print[mul_array.tolist[]]

Output

Multiplying all elements of the list by constant using NumPy

Conclusion

These are the methods to multiply all the elements in the list in python. You can use all the methods in your code. But I will suggest you go with the fourth method which is using NumPy as its calculation is fast as compared to others.

I hope you have liked this tutorial and understood it. If you have any queries then you can contact us for more help.

Join our list

Subscribe to our mailing list and get interesting stuff and updates to your email inbox.

We respect your privacy and take protecting it seriously

Thank you for signup. A Confirmation Email has been sent to your Email Address.

Something went wrong.

How do you multiply all elements in a list by constant Python?

Use the syntax [element * number for element in list] to multiply each element in list by number ..
a_list = [1, 2, 3].
multiplied_list = [element * 2 for element in a_list].
print[multiplied_list].

How do you multiply an array in Python?

multiply[] function is used when we want to compute the multiplication of two array. It returns the product of arr1 and arr2, element-wise.

How do you multiply every element in a NumPy array by a scalar?

Numpy multiply array by scalar In order to multiply array by scalar in python, you can use np. multiply[] method.

How do you multiply all values in a NumPy array?

A Quick Introduction to Numpy Multiply You can use np. multiply to multiply two same-sized arrays together. This computes something called the Hadamard product. In the Hadamard product, the two inputs have the same shape, and the output contains the element-wise product of each of the input values.

Chủ Đề