Elementwise multiplication of two lists in Python

Hello programmers, in this tutorial we will see how to multiply two lists and get their product-wise elementwise in Python.

There can be a possibility where the programmer may have to use the elementwise multiplication of two lists for various purposes. There are many ways to do this process in Python. We will see some of the ways in this tutorial.

There are many methods we can use to multiply the list elementwise. We shall see them in order starting from the simple approach of doing this.

Method 1 – Using a simple ‘FOR’ loop – elementwise list multiplication in Python

Given below is the illustration of using a simple ‘FOR’ loop to multiply the lists.

#elementwise multiplication of two lists function
def eleMulList(list1, list2):
    if len(list1) != len(list2):
        return "The lists are not of equal length."
    resList = []
    for i in range(len(list1)):
        resList.append(list1[i] * list2[i])
    return resList

#driver
if "__main__":
    list1 = [3,8,9,12,5,7]
    list2 = [2,0,1,4,13,17]
    print(f'The elementwise multiplication of {list1} and {list2} is: {eleMulList(list1, list2)}.')

Output

The elementwise multiplication of [3, 8, 9, 12, 5, 7] and [2, 0, 1, 4, 13, 17] is: [6, 0, 9, 48, 65, 119].

Explanation
Inside the eleMulList() function, we first check if the length of the lists is of equal length or not as the multiplication will not be possible if they are of different sizes. We iterate over both the lists using a for loop and add the element multiplication of the values in a new result list.

Method 2 – Using List Comprehension

Given below is the illustration of multiplying two lists using list comprehension.

#elementwise multiplication of two lists function
def eleMulList(list1, list2):
    if len(list1) != len(list2):
        return "The lists are not of equal length."
    return [list1[i]* list2[i] for i in range(len(list1))]

#driver
if "__main__":
    list1 = [3,8,9,12,5,7]
    list2 = [2,0,1,4,13,17]
    print(f'The elementwise multiplication of {list1} and {list2} is: {eleMulList(list1, list2)}.')

Output

The elementwise multiplication of [3, 8, 9, 12, 5, 7] and [2, 0, 1, 4, 13, 17] is: [6, 0, 9, 48, 65, 119].

Explanation
Inside the eleMulList() function, we again check if the length of the lists is of equal length or not as the multiplication will not be possible if they are of different sizes. We return the elementwise multiplication values using a for loop again using it in a list comprehension.

Method 3 – Using a Numpy array

Before we check out this approach, we need to install the Python NumPy module. For this, type the following command in the command prompt of your system to install the NumPy library.

pip install numpy

It will automatically download the latest NumPy library.

Given below is the illustration of how to use a NumPy array for elementwise multiplication of elements.

#Importing library
import numpy as np

#elementwise multiplication of two lists function
def eleMulList(list1, list2):
    list1 = np.array(list1)
    list2 = np.array(list2)
    if len(list1) != len(list2):
        return "The lists are not of equal length."
    return [list1[:len(list1)] * list2[:len(list2)]]

#driver
if "__main__":
    list1 = [3,8,9,12,5,7]
    list2 = [2,0,1,4,13,17]
    print(f'The elementwise multiplication of {list1} and {list2} is: {eleMulList(list1, list2)}.')

Output

The elementwise multiplication of [3, 8, 9, 12, 5, 7] and [2, 0, 1, 4, 13, 17] is: [6, 0, 9, 48, 65, 119].

Explanation
Inside the eleMulList() function, both the lists are converted into NumPy arrays in the beginning and then next we check if the length of the arrays is the same or not. If the lengths of both arrays are the same, we proceed by multiplying both the list elements and returning the resultant list from the function. For this, the NumPy library is imported.

Another approach uses the inbuilt function in the NumPy library.

#Importing library
import numpy as np

#elementwise multiplication of two lists function
def eleMulList(list1, list2):
    list1 = np.array(list1)
    list2 = np.array(list2)
    if len(list1) != len(list2):
        return "The lists are not of equal length."
    return np.multiply(list1,list2)

#driver
if "__main__":
    list1 = [3,8,9,12,5,7]
    list2 = [2,0,1,4,13,17]
    print(f'The elementwise multiplication of {list1} and {list2} is: {eleMulList(list1, list2)}.')

Output

The elementwise multiplication of [3, 8, 9, 12, 5, 7] and [2, 0, 1, 4, 13, 17] is: [6, 0, 9, 48, 65, 119].

Explanation
Here we use the inbuilt function ‘.multiply’ for doing the elementwise multiplication of the two lists.

Leave a Reply

Your email address will not be published.