While working with the python lists, we can come across a situation in which we require to exponent constant for each element in the list. We possibly need to iterate an exponent constant to each element but that would increase the line of code. Let’s discuss certain shorthands to perform this task.
Method #1: Using List Comprehension List comprehension is just the short way to perform the task we perform using the naive method. This is mainly useful to save time and also is best among others when it comes to the readability of the code.
Python3
# Python3 code to demonstrate # Exponentiation by K in list # using list comprehension # initializing list test_list = [ 4 , 5 , 6 , 3 , 9 ] # printing original list print ("The original list is : " + str (test_list)) # initializing K K = 4 # using list comprehension # Exponentiation by K in list res = [x * * K for x in test_list] # printing result print ("The list after constant exponentiation : " + str (res)) |
The original list is : [4, 5, 6, 3, 9] The list after constant exponentiation : [256, 625, 1296, 81, 6561]
Time Complexity: O(n), where n is the length of the given list.
Auxiliary Space: O(n)
Method #2 : Using map() + operator.pow This is similar to the above function but uses the operator.pow to exponent each element to other elements from the other list of K formed before applying the map function. It powers the similar index elements of list.
Python3
# Python3 code to demonstrate # Exponentiation by K in list # using map() + operator.pow import operator # initializing list test_list = [ 4 , 5 , 6 , 3 , 9 ] # printing original list print ("The original list is : " + str (test_list)) # initializing K list K_list = [ 4 ] * len (test_list) # using map() + operator.pow # Exponentiation by K in list res = list ( map (operator. pow , test_list, K_list)) # printing result print ("The list after constant exponentiation : " + str (res)) |
The original list is : [4, 5, 6, 3, 9] The list after constant exponentiation : [256, 625, 1296, 81, 6561]
Time complexity: O(n) where n is the length of the input list “test_list”. This is because we are performing exponentiation operation on each element in the list, so the time complexity would be proportional to the number of elements in the list.
Auxiliary Space: O(n) where n is the length of the input list “test_list”. This is because we are creating a new list “res” which stores the result of exponentiation of each element in “test_list”.
Method #3 : Using numpy
Note: Install numpy module using command “pip install numpy”
Python3
import numpy as np test_list = [ 4 , 5 , 6 , 3 , 9 ] # Using numpy power function to perform exponentiation on the list with a constant value res = np.power(test_list, 4 ) # print result print ( "The list after constant exponentiation : " + str (res)) #This code is contributed by Edula Vinay Kumar Reddy |
Output:
The list after constant exponentiation : [ 256 625 1296 81 6561]
Time complexity: O(n)
Auxiliary Space: O(n)
Method #4 : Using math.pow() method
Python3
# Python3 code to demonstrate # Exponentiation by K in list # using list comprehension # initializing list test_list = [ 4 , 5 , 6 , 3 , 9 ] # printing original list print ( "The original list is : " + str (test_list)) # initializing K K = 4 # using list comprehension # Exponentiation by K in list import math res = [ int (math. pow (x,K)) for x in test_list] # printing result print ( "The list after constant exponentiation : " + str (res)) |
The original list is : [4, 5, 6, 3, 9] The list after constant exponentiation : [256, 625, 1296, 81, 6561]
Time complexity: O(n)
Auxiliary Space: O(n)
Method 5: Using map() + lambda function
The map() function applies the lambda function to each element of the list and returns a map object, which can be converted to a list by using the list() constructor. The lambda function takes an argument x and returns its exponentiation by the constant K.
Below is the implementation:
Python3
# initializing list test_list = [ 4 , 5 , 6 , 3 , 9 ] # printing original list print ( "The original list is : " + str (test_list)) # initializing K K = 4 # using map() and lambda function # Exponentiation by K in list res = list ( map ( lambda x: x * * K, test_list)) # printing result print ( "The list after constant exponentiation : " + str (res)) |
The original list is : [4, 5, 6, 3, 9] The list after constant exponentiation : [256, 625, 1296, 81, 6561]
Time Complexity: O(n)
Auxiliary Space: O(n)
Method 6: Using a loop:
- Create an empty list named res to store the results of exponentiation.
- Start a for loop that iterates over each element in the test_list.
- Raise the current element x to the power of K using the ** operator and append the result to the res list using the append() method.
- Print the resulting list of exponentiated values as a string using the str() function and the print() function.
Python3
test_list = [ 4 , 5 , 6 , 3 , 9 ] K = 4 res = [] # iterate over each element in the list for x in test_list: # raise the element to the power of K and append to result list res.append(x * * K) # print the resulting list print ( "The list after constant exponentiation : " + str (res)) |
The list after constant exponentiation : [256, 625, 1296, 81, 6561]
Time Complexity: O(n), where n is the length of the given test_list.
Auxiliary Space: O(n)