How To Multiply Two Lists Element-Wise In Python

Multiply two lists element-wise in Python

“Multiply two lists element-wise” in Python means multiply the element of one list with the element in the corresponding position in another list. So now, let’s look at three methods below for this question.

Multiply two lists element-wise in Python

Assume I have two lists:

list1 = [5, 2, 5, 4, 5, 8]
list2 = [1, 5, 3, 5, 5]

And my expected result after multiplying two lists:

result = [5, 10, 15, 20, 25]

Using For Loop

Syntax:

for value in sequence:
    #loop body

Parameters:

  • value: represents the element of the sequence on each iteration.
  • sequence: an iterable datatype such as a list, a tuple, a set, or a string.

We can use the For Loop to go through all the elements in the smaller list. After that, we multiply the elements of two lists and then append them to the empty list. Like this:

list1 = [5, 2, 5, 4, 5, 8]
list2 = [1, 5, 3, 5, 5]
result = []

# Find the length of the smaller list
smallerListLen = len(list1) if len(list1) < len(list2) else len(list2)

for i in range(0, smallerListLen):
    result.append(list1[i] * list2[i])

print(result)

Output:

[5, 10, 15, 20, 25]

Using map() function

Syntax:

map(function, iterable)

Parameters:

  • function: the function that is called on each iteration to execute for each item.
  • iterable: an object that can iterate over, such as a list, a tuple, a set, or a string.

First, we define a multiply() function that gets the results from multiplying two numbers. Then we pass the multiply() function and two lists into the map() function. The map() function will automatically pass the elements in the same position of two lists into the multiply() function. Finally, the return value of map() is a map object. Convert it to a list, and we’ve done.

list1 = [5, 2, 5, 4, 5, 8]
list2 = [1, 5, 3, 5, 5]

def multiply(a, b):
    return a * b

result = list(map(multiply, list1, list2))
print(result)

We can also use lambda to shorten the source code like this:

list1 = [5, 2, 5, 4, 5, 8]
list2 = [1, 5, 3, 5, 5]
result = list(map(lambda a, b: a * b, list1, list2))

print(result)

Output:

[5, 10, 15, 20, 25]

Using zip() functions

Syntax:

zip(iterable, iterable, iterable...)

Parameters:

  • iterable: an object that one can iterate over (like list, string)

The zip() function takes two or more lists and combines elements with the same index into a zip object. The zip element is a tuple, so we can iterate over the elements of the zip object and get the result of the multiplication of 2 numbers in the tuple by using the list comprehension.

list1 = [5, 2, 5, 4, 5, 8]
list2 = [1, 5, 3, 5, 5]
result = [a * b for a, b in zip(list1, list2)]

print(result)

Output:

[5, 10, 15, 20, 25]

Summary

You have read and understood three ways to multiply two lists element-wise in Python. Of course, this is just basic knowledge, but it will help you build a solid foundation for the future of learning Python programming. Thank you for reading!

Maybe you are interested:

Leave a Reply

Your email address will not be published. Required fields are marked *