Python Program to Find Product of List | Previously, we will add the list in python. In this program, we will find the product of a list. Like, the sum of lists we can perform multiplication too, In this section, we see some codes for python list multiplication by different methods.
We will see these below Python program examples:–
- How To Multiply Two Lists In Python
- Python Multiply List By Scalar
- Python Multiply List By Float
- Multiply All Elements In List Python
- Python Multiply All Elements In List By Constant
- Multiply Each Element In List Python Numpy
How to Multipy Two List in Python
Now, we will see a simple program to multiply two lists with corresponding elements.
list1 = [5,6,4,3] list2 = [3,5,3,3] print("List1:", str(list1)) print("List2:", str(list2)) result =  for i in range(0, len(list1)): result.append(list1[i] * list2[i]) print("Product:", str(result))
List1: [5, 6, 4, 3]
List2: [3, 5, 3, 3]
Product: [15, 30, 12, 9]
We have initialized two lists and initialized the third list that results in an empty list then use for loop over the range 0 to the length of the list and iterate it over all elements and multiply corresponding elements.
Python Program to Find Product of List by Sclar
Scalar multiplication is a multiplication of a vector by a scalar, it is a function from K X V to V where K is a field and V is a vector space over K. Syntax for scalar multiplication is as follows: [element * number of elements in list]
list = [4,6,7] result = [element * 2 for element in list] print(result)
So, here we multiply each element in a list with 2 hence the result is as follows.
Python Program to Multiply List by Float
We can multiply float value to the list, let us see the example code for this.
list1 = [5,7,4,8] result =  for i in range(0, len(list1)): result.append(list1[i] * 1.2) print(str(result))
[6.0, 8.4, 4.8, 9.6]
The above code works as follows:-
Step1: First we initialize some values to the list
Step2: Then we print the list
Step3: Next we initialize the result to an empty list
Step4: Then in the for loop, we iterate over range 0 to the length of a list, and append the float multiplication to the result.
Step5: Print the result.
Multiply All Elements in List Python
Now, we multiply all the elements in a single list. This python program multiples all the ist elements and prints the output, unlike other programs we need only one list for this code.
list = [9,7,5] result = 1 for i in list: result = result * i print(result)
So, in the above code, we have initialized the list to value and then result to 1, usually we initialize the result to 0, as this is multiplication we use 1. Then in for loop, we iterate over the list to multiply each element and store it in the result. Finally, print the result.
Python Multiply All Elements in a List by Constant
This works similar to the scalar multiplication, here we can initialize constant to some value.
list = [4,3,3] a = 4 result = [b * a for b in list] print(result)
We initialize the list, print the same then initialize a constant to some number, then in result, we multiply a list with constant, by iterating through for loop. Print the result.
Multiply Each Element in the List Python Numpy
Numpy is a built-in library in python, which has many functions, In this code, we use one such function.
import numpy as np l1 = [1,2,3] l2 = [3,4,5] print("List1:", l1) print("List2:", l2) result = np.multiply(l1, l2) print("Product:", result)
List1: [1, 2, 3]
List2: [3, 4, 5]
Product: [ 3 8 15]
In this code, we have imported NumPy as np and then initialized list1 and list2 the multiply two results using np.multiply() which takes two parameters list1 and list2, then print the result.
If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!