In this tutorial, we are going to discuss some methods which are used to multiply two lists in Python. This tutorial has some unique and simple methods to perform the multiplication of both lists. This tutorial will demonstrate various methods to perform the element-wise multiplication of two lists in Python. Suppose we have two lists of integers with the same size and we want to perform the multiplication of elements of the first list with the elements at the same index in the second list and get a resultant list with the same size.
Some of the common ways to multiply two lists are listed below.
- Multiply two lists using For Loop
- Multiplying two Lists using List Comprehension
- Multiplication of two lists using Numpy
- Multiply Both Lists using the Naive technique
If you haven’t seen our tutorial for List Multiplication and Product of Lists in Python, also take a look at them for a better understanding.
Multiply two lists using for loop
We can simply get the product of two lists using for loop. Through for loop, we can iterate through the list. Similarly, with every iteration, we can multiply the elements from both lists. For this purpose, we can use Zip Function. The zip() function in python can combine the contents of 2 or more iterables. Zip Function returns a zipped output. We can then simply store the output in Result and Display it on the console. This is a very simple way to perform list multiplication in Python.
#Using for loop
list1 = [1, 2, 3, 4, 5]
list2 = [6, 5, 4, 3, 3]
Result = []
for i1, i2 in zip(list1, list2):
Result.append(i1*i2)
print("The product of 2 lists is: ", Result)
Output:
The Product of 2 lists is: [6, 10, 12, 12, 15]
Now another interesting situation arises if list 1 has more elements than list 2. In this case, the zip() function helps to discard the last element and make sure that every element is multiplied by the other element of another list with the same index number.
#Using for loop
list1 = [1, 2, 3, 4, 5, 6]
list2 = [6, 5, 4, 3, 3]
Result = []
for i1, i2 in zip(list1, list2):
Result.append(i1*i2)
print("The product of 2 lists is: ", Result)
Output:
The product of 2 lists is: [6, 10, 12, 12, 15]
Multiply two lists using List comprehension
List comprehension is another method for multiplying both lists in python. Let’s see how it works. This is a very simple code that requires no complex syntax. If you don’t know about List Comprehension, Visit this.
#Using list comprehension
list_1 = [9, 8, 7]
list_2 = [1, 2, 3]
result = [i1 * i2 for i1, i2 in zip(list_1, list_2)]
print(result)
Output
[9, 16, 21]
Since we are using Zip() Function here, there is no need to worry about any of the elements in both lists. Zip Function will multiply the elements with each other as long as the last element of the shortest list is reached.
Multiply two lists using numpy
First of all, we will have to import the Numpy library. Secondly, we will find the list which has the minimum number of elements in it. Then we will convert these lists into NumPy. And Lastly, we will multiply the elements of list 1 by the elements of list 2 with the same index numbers.
#Using Numpy
import numpy as x
list1 = [1, 2, 3, 4, 5]
list2 = [9, 8, 7, 6, 5, 6]
n = min(len(list1), len(list2))
array1 = x.array(list1)
array2 = x.array(list2)
result = array1[:n]*array2[:n]
result = result.tolist()
print("The product of both lists is: ", result)
Output
The product of both lists is: [9, 16, 21, 24, 25]
Multiply Both Lists using the Naive technique
Here is another interesting method to multiply both lists. Let’s discuss it. This is again a very simple method. We have used for loop here to multiply the elements of both lists with the same index number.
#Using Naive technique
list1 = [1, 3, 4, 6, 8]
list2 = [4, 5, 6, 2, 10]
result = []
for x in range(0, len(list1)):
result.append(list1[x] * list2[x])
print ("Product of both lists is : " + str(result))'
Output
Product of both lists is : [4, 15, 24, 12, 80]
Please leave feedback here. Make sure to see our other tutorials to learn Python and Matlab.
See more Python List Tutorials