In this tutorial, we will be discussing how to Multiply two Lists in Python. We will discuss various approaches for list Multiplication. This tutorial has some unique and simple methods to perform the multiplication of both lists.
Some of the Methods we will discuss here are:
- List Multiplication in Python using For Loop
- Multiply two Lists in Python using List Comprehension
- Product of two lists using Numpy
- Multiply Both Lists using Naive technique
Multiply two lists using for loop – python
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.
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)
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.
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)
product 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.
list_1 = [9, 8, 7] list_2 = [1, 2, 3] result = [i1 * i2 for i1, i2 in zip(list_1, list_2)] print(result)
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.
Product of two lists using numpy – python
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 to the elements of list 2 with the same index numbers.
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)
Multiply Both Lists using 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.
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))'
If you liked our tutorial about how to multiply two lists in python, please leave feedback below. Make sure to see our other tutorials to learn Python and Matlab.
See more Python List Tutorials