Python | Exponentiation by K in list
Last Updated :
27 Feb, 2023
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
test_list = [ 4 , 5 , 6 , 3 , 9 ]
print ("The original list is : " + str (test_list))
K = 4
res = [x * * K for x in test_list]
print ("The list after constant exponentiation : " + str (res))
|
Output :
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
import operator
test_list = [ 4 , 5 , 6 , 3 , 9 ]
print ("The original list is : " + str (test_list))
K_list = [ 4 ] * len (test_list)
res = list ( map (operator. pow , test_list, K_list))
print ("The list after constant exponentiation : " + str (res))
|
Output :
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 ]
res = np.power(test_list, 4 )
print ( "The list after constant exponentiation : " + str (res))
|
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
test_list = [ 4 , 5 , 6 , 3 , 9 ]
print ( "The original list is : " + str (test_list))
K = 4
import math
res = [ int (math. pow (x,K)) for x in test_list]
print ( "The list after constant exponentiation : " + str (res))
|
Output
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
test_list = [ 4 , 5 , 6 , 3 , 9 ]
print ( "The original list is : " + str (test_list))
K = 4
res = list ( map ( lambda x: x * * K, test_list))
print ( "The list after constant exponentiation : " + str (res))
|
Output
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 = []
for x in test_list:
res.append(x * * K)
print ( "The list after constant exponentiation : " + str (res))
|
Output
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)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...