Open In App

Python – Product of i^k in List

Last Updated : 18 May, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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




# Python code to demonstrate
# Product of i ^ k in List
# using reduce() + lambda + pow()
 
# initializing list
test_list = [1, 3, 5, 7, 9, 11]
 
# printing original list
print ("The original list is : " + str(test_list))
 
# initializing K
K = 4
 
# using reduce() + lambda + pow()
# Product of i ^ k in List
res = reduce(lambda i, j: i * pow(j, K), [pow(test_list[:1][0], K)] + test_list[1:])
 
# printing result
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




# Python3 code to demonstrate
# Product of i ^ k in List
# using map() + loop + pow()
 
# getting Product
def prod(val) :
    res = 1
    for ele in val:
        res *= ele
    return res 
 
# initializing list
test_list = [3, 5, 7, 9, 11]
 
# printing original list
print ("The original list is : " + str(test_list))
 
# initializing K
K = 4
 
# using map() + loop + pow()
# Product of i ^ k in List
res = prod(map(lambda i : pow(i, K), test_list))
 
# printing result
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

  1. Initiate a for loop to access list elements
  2. Raise each element to the power of K and then find the product
  3. Display the product

Python3




# Python3 code to demonstrate
# Product of i ^ k in List
 
# getting Product
 
 
# initializing list
test_list = [3, 5, 7, 9, 11]
 
# printing original list
print ("The original list is : " + str(test_list))
 
# initializing K
K = 4
 
# Product of i ^ k in List
res=1
for i in test_list:
    res*=i**K
 
# printing result
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
 
# initializing list
test_list = [3, 5, 7, 9, 11]
 
# initializing K
K = 4
 
# Product of i ^ k in List using numpy.prod()
res = np.prod(np.power(test_list, K))
 
# printing original list
print("The original list is : " + str(test_list))
 
# printing result
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




# Recursive function to calculate the product of i^k
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)
 
# initializing list
test_list = [3, 5, 7, 9, 11]
 
# initializing K
K = 4
 
# calculating product of i ^ k using recursion
res = product_recursive(test_list, 0, K)
 
# printing original list
print("The original list is: " + str(test_list))
 
# printing result
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
Previous
Next
Share your thoughts in the comments

Similar Reads