Python | Product of Squares in List
Last Updated :
23 Apr, 2023
Python being the language of magicians can be used to perform many tedious and repetitive tasks in an easy and concise manner and have the knowledge to utilize this tool to the fullest is always useful. One such small application can be finding product of squares of list in just one line. Let’s discuss certain ways in which this can be performed.
Method #1: Using reduce() + lambda The power of lambda functions to perform lengthy tasks in just one line, allows it combined with reduce which is used to accumulate the subproblem, to perform this task as well. Works with only Python 2.
Python3
import functools
test_list = [ 3 , 5 , 7 , 9 , 11 ]
print ( "The original list is : " + str (test_list))
res = functools. reduce ( lambda i, j: i * j * j, [test_list[: 1 ][ 0 ] * * 2 ] + test_list[ 1 :])
print ( "The product of squares of list is : " + str (res))
|
Output
The original list is : [3, 5, 7, 9, 11]
The product of squares of list is : 108056025
Time Complexity: O(n), where n is the number of elements in the list.
Auxiliary Space: O(1), as only a single variable ‘res’ is used to store the result and no extra data structures are used.
Method #2: Using map() + loop The similar solution can also be obtained using the map function to integrate and external product function to perform the product of the squared number.
Python3
def prod(val) :
res = 1
for ele in val:
res * = ele
return res
test_list = [ 3 , 5 , 7 , 9 , 11 ]
print ( "The original list is : " + str (test_list))
res = prod( map ( lambda i : i * i, test_list))
print ( "The product of squares of list is : " + str (res))
|
Output
The original list is : [3, 5, 7, 9, 11]
The product of squares of list is : 108056025
Time complexity: O(n) where n is no of elements of the list
Auxiliary Space: O(1)
Method #3: Using for loop
Python3
test_list = [ 3 , 5 , 7 , 9 , 11 ]
print ( "The original list is : " + str (test_list))
res = 1
for i in test_list:
x = i * * 2
res * = x
print ( "The product of squares of list is : " + str (res))
|
Output
The original list is : [3, 5, 7, 9, 11]
The product of squares of list is : 108056025
Time Complexity: O(n), where n is the length of the list test_list.
Auxiliary Space: O(1), as only a few variables are used in the computation.
Method #4: Using NumPy library
Note: Install numpy module using command “pip install numpy”
Python3
import numpy as np
test_list = [ 3 , 5 , 7 , 9 , 11 ]
print ( "The original list is : " + str (test_list))
res = np.prod(np.square(test_list))
print ( "The product of squares of list is : " + str (res))
|
Output:
The original list is : [3, 5, 7, 9, 11]
The product of squares of list is : 108056025
Time Complexity: O(n) where n is no of elements of the list
Auxiliary Space: O(n)
METHOD 5:Using pow function
APPROACH:
In this program, we are calculating the product of squares of elements in a list. We are iterating through the list, calculating the square of each element and then multiplying it with the current product value. Finally, we are printing the product of squares of all the elements in the list.
ALGORITHM:
1.Initialize a variable ‘product’ to 1.
2.Iterate through the elements of the list using a for loop.
3.Calculate the square of each element using the pow() function.
4.Multiply the square value with the current product value.
5.After the loop completes, the product variable will contain the product of squares of all the elements in the list.
6.Print the product value.
Python3
lst = [ 3 , 5 , 7 , 9 , 11 ]
product = 1
for num in lst:
product * = pow (num, 2 )
print ( "The product of squares of list is :" , product)
|
Output
The product of squares of list is : 108056025
Time Complexity: O(n), where n is the number of elements in the list.
Space Complexity: O(1), as we are not using any extra space apart from the input list and the product variable.
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...