Python – Product of i^k in List
Last Updated :
18 May, 2023
Python being the language of magicians can be used to perform many tedious and repetitive tasks in a easy and concise manner and having the knowledge to utilize this tool to the fullest is always useful. One such small application can be finding product of i^k of list in just one line. Let’s discuss certain ways in which this can be performed.
Method #1 : Using reduce() + lambda + pow() 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. The pow() is used to perform task of computing power. Works with only Python 2.
Python
test_list = [ 1 , 3 , 5 , 7 , 9 , 11 ]
print ("The original list is : " + str (test_list))
K = 4
res = reduce ( lambda i, j: i * pow (j, K), [ pow (test_list[: 1 ][ 0 ], K)] + test_list[ 1 :])
print ("The product of i ^ k of list is : " + str (res))
|
Output :
The original list is : [1, 3, 5, 7, 9, 11]
The product of i ^ k of list is : 11676104538800625
Time Complexity: O(n), where n is the number of elements in the list “test_list”.
Auxiliary Space: O(1), constant extra space is required
Method #2 : Using map() + loop + pow() The similar solution can also be obtained using the map function to integrate and prod function to perform the product of the i^k 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))
K = 4
res = prod( map ( lambda i : pow (i, K), test_list))
print ("The product of i ^ k of list is : " + str (res))
|
Output :
The original list is : [1, 3, 5, 7, 9, 11]
The product of i ^ k of list is : 11676104538800625
Method #3 : Using ** and * operators
Approach
- Initiate a for loop to access list elements
- Raise each element to the power of K and then find the product
- Display the product
Python3
test_list = [ 3 , 5 , 7 , 9 , 11 ]
print ( "The original list is : " + str (test_list))
K = 4
res = 1
for i in test_list:
res * = i * * K
print ( "The product of i ^ k of list is : " + str (res))
|
Output
The original list is : [3, 5, 7, 9, 11]
The product of i ^ k of list is : 11676104538800625
Time Complexity : O(N)
Auxiliary Space : O(N)
Method #4: Using numpy.prod() function
This method involves using the numpy library in Python to compute the product of the list elements raised to the power of k.
Steps:
Import numpy library.
Initialize the list and k value.
Use numpy.prod() function to compute the product of the list elements raised to the power of k.
Print the result.
Python3
import numpy as np
test_list = [ 3 , 5 , 7 , 9 , 11 ]
K = 4
res = np.prod(np.power(test_list, K))
print ( "The original list is : " + str (test_list))
print ( "The product of i ^ k of list is : " + str (res))
|
OUTPUT :
The original list is : [3, 5, 7, 9, 11]
The product of i ^ k of list is : 11676104538800625
Time complexity: O(n)
Auxiliary space: O(1)
Method #5 : Using recursion
- Create a recursive function, let’s call it product_recursive, that takes the test_list, the index i, and the exponent K as parameters.
- In the function, check the base case: if i is equal to the length of test_list, return 1.
- Otherwise, calculate the product recursively by multiplying the element at index i raised to the power of K with the result of the recursive call to product_recursive with i+1.
- Call the product_recursive function initially with i=0.
- Assign the result to the res variable.
- Print the original list and the result.
Python3
def product_recursive(test_list, i, K):
if i = = len (test_list):
return 1
else :
return test_list[i] * * K * product_recursive(test_list, i + 1 , K)
test_list = [ 3 , 5 , 7 , 9 , 11 ]
K = 4
res = product_recursive(test_list, 0 , K)
print ( "The original list is: " + str (test_list))
print ( "The product of i ^ k of list is: " + str (res))
|
Output
The original list is: [3, 5, 7, 9, 11]
The product of i ^ k of list is: 11676104538800625
Time complexity: The time complexity of this method is O(n), where n is the length of the test_list.
Auxiliary space: The auxiliary space complexity is O(n) due to the recursive calls, where n is the length of the test_list.
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...