How to multiply an array by a constant in python

Multiplying a constant to a NumPy array is as easy as multiplying two numbers. To multiply a constant to each and every element of an array, use multiplication arithmetic operator *. To multiplication operator, pass array and constant as operands as shown below.

b = a * c

Run

where a is input array and c is a constant. b is the resultant array.

Example

In the following python example, we will multiply a constant 3 to an array a. The resulting array is stored in b.

import numpy as np

#2D array
a = (np.arange(8)*2).reshape(2,4)

#print array
print("The array\n",a)

#multiplying a constant to all the elements of array
b = a * 3

print("\nAfter multiplying a constant to all the elements of array\n",b)

Run

How to multiply an array by 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.]])

View Discussion

Improve Article

Save Article

  • Read
  • Discuss
  • View Discussion

    Improve Article

    Save Article

    While working with the python lists, we can come over a situation in which we require to multiply constant to each element in the list. We possibly need to iterate and multiply constant to each element but that would increase the line of code. Let’s discuss certain shorthands to perform this task.

    Method #1 : Using List Comprehension 
    List comprehension is just the short way to perform the task we perform using the naive method. This is mainly useful to save time and also is best among others when it comes to the readability of the code.

    Python3

    test_list = [4, 5, 6, 3, 9]

    print ("The original list is : " + str(test_list))

    K = 4

    res = [x * K for x in test_list]

    print ("The list after constant multiplication : " + str(res))

    Output

    The original list is : [4, 5, 6, 3, 9]
    The list after constant multiplication : [16, 20, 24, 12, 36]

     
    Method #2 : Using map() + operator.mul 
    This is similar to the above function but uses the operator.mul to multiply each element to other element from the other list of K formed before applying the map function. It multiplies the similar index elements of list.

    Python3

    import operator

    test_list = [4, 5, 6, 3, 9]

    print ("The original list is : " + str(test_list))

    K_list = [4] * len(test_list)

    res = list(map(operator.mul, test_list, K_list))

    print ("The list after constant multiplication : " + str(res))

    Output

    The original list is : [4, 5, 6, 3, 9]
    The list after constant multiplication : [16, 20, 24, 12, 36]
    


    Can you multiply an array with a constant?

    When we multiply an array by a constant, each element is multiplied by that constant.

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

    To multiply a constant to each and every element of an array, use multiplication arithmetic operator * . To multiplication operator, pass array and constant as operands as shown below. where a is input array and c is a constant. b is the resultant array.

    How do you multiply an array in Python?

    multiply() in Python. numpy. 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 a constant in a list Python?

    To multiply each element in a list by a number:.
    Declare a new variable that stores an empty list..
    Use a for loop to iterate over the original list..
    On each iteration, multiply the current list item by the number..
    Append the result to the new list..