Python – Cross list exponentiation
Sometimes we come across situations in which we require to apply a particular function to each element of two lists at similar indices. These are quite similar and come up as an application for certain utilities. Let’s discuss certain ways in which the exponentiation, i.e remainder of two lists can be performed.
Method #1: Using zip() + list comprehension The zip operation can be used to link one list with the other and the computation part can be handled by the list comprehension and hence providing a shorthand to this particular problem.
Python3
test_list1 = [ 3 , 5 , 2 , 6 , 4 ]
test_list2 = [ 7 , 3 , 4 , 1 , 5 ]
print ("The original list 1 is : " + str (test_list1))
print ("The original list 2 is : " + str (test_list2))
res = [i * * j for i, j in zip (test_list1, test_list2)]
print ("The cross exponentiation list is : " + str (res))
|
Output :
The original list 1 is : [3, 5, 2, 6, 4]
The original list 2 is : [7, 3, 4, 1, 5]
The cross exponentiation list is : [2187, 125, 16, 6, 1024]
Time Complexity: O(n), where n is the length of the lists test_list1 and test_list2
Auxiliary Space: O(n), as it creates a new list of size n to store the result.
Method #2: Using map() Using map function is the most elegant way in which we can possibly perform the twining of a function with both lists. Different operations other than exponentiation can also be applied to it.
Python3
from operator import pow
test_list1 = [ 3 , 5 , 2 , 6 , 4 ]
test_list2 = [ 7 , 3 , 4 , 1 , 5 ]
print ("The original list 1 is : " + str (test_list1))
print ("The original list 2 is : " + str (test_list2))
res = list ( map ( pow , test_list1, test_list2))
print ("The cross exponentiation list is : " + str (res))
|
Output :
The original list 1 is : [3, 5, 2, 6, 4]
The original list 2 is : [7, 3, 4, 1, 5]
The cross exponentiation list is : [2187, 125, 16, 6, 1024]
Time complexity: O(n), where n is the length of the input lists “test_list1” and “test_list2”. This is because the map() function iterates over each element of the lists and performs exponentiation operation in O(1) time for each element.
Auxiliary space: O(n), where n is the length of the resultant list. This is because the resultant list needs to be stored in memory.
Method #3: Using math.pow() method
Python3
test_list1 = [ 3 , 5 , 2 , 6 , 4 ]
test_list2 = [ 7 , 3 , 4 , 1 , 5 ]
print ( "The original list 1 is : " + str (test_list1))
print ( "The original list 2 is : " + str (test_list2))
res = []
for i in range ( 0 , len (test_list1)):
import math
x = math. pow (test_list1[i],test_list2[i])
res.append( int (x))
print ( "The cross exponentiation list is : " + str (res))
|
Output
The original list 1 is : [3, 5, 2, 6, 4]
The original list 2 is : [7, 3, 4, 1, 5]
The cross exponentiation list is : [2187, 125, 16, 6, 1024]
Time Complexity: O(N)
Auxiliary Space: O(N)
Method #4: Using numpy.power()
Note: Install numpy module using command “pip install numpy”
The numpy module in Python is a powerful library and provides us with the numpy.power() method which takes in two arrays as parameters and returns a new array with each element of the first array raised to the corresponding element of the second array.
Python3
import numpy as np
test_list1 = [ 3 , 5 , 2 , 6 , 4 ]
test_list2 = [ 7 , 3 , 4 , 1 , 5 ]
print ( "The original list 1 is : " + str (test_list1))
print ( "The original list 2 is : " + str (test_list2))
res = np.power(test_list1, test_list2)
print ( "The cross exponentiation list is : " + str (res))
|
Output :
The original list 1 is : [3, 5, 2, 6, 4]
The original list 2 is : [7, 3, 4, 1, 5]
The cross exponentiation list is : [2187 125 16 6 1024]
Time Complexity: O(N)
Auxiliary Space: O(N) as it creates a new list of size N to store the result.
Method 5: Using a for loop: You can use a for loop to iterate over the indices of the lists and calculate the cross exponentiation.
Python3
test_list1 = [ 3 , 5 , 2 , 6 , 4 ]
test_list2 = [ 7 , 3 , 4 , 1 , 5 ]
res = []
for i in range ( len (test_list1)):
res.append(test_list1[i] * * test_list2[i])
print ( "The cross exponentiation list is : " , res)
|
Output
The cross exponentiation list is : [2187, 125, 16, 6, 1024]
Time Complexity: O(N)
Auxiliary Space: O(N) as it creates a new list of size N to store the result.
Method #6: Using a list comprehension and a lambda function
This method uses a lambda function in a list comprehension to perform the exponentiation operation on corresponding elements of the two lists. The zip() function is used to iterate over the two lists simultaneously.
Python3
test_list1 = [ 3 , 5 , 2 , 6 , 4 ]
test_list2 = [ 7 , 3 , 4 , 1 , 5 ]
print ( "The original list 1 is : " + str (test_list1))
print ( "The original list 2 is : " + str (test_list2))
res = [( lambda x, y: x * * y)(x, y) for x, y in zip (test_list1, test_list2)]
print ( "The cross exponentiation list is : " + str (res))
|
Output
The original list 1 is : [3, 5, 2, 6, 4]
The original list 2 is : [7, 3, 4, 1, 5]
The cross exponentiation list is : [2187, 125, 16, 6, 1024]
Time Complexity: O(N), where N is the length of given test_list1 and test_list2
Auxiliary Space: O(N) as it creates a new list of size N to store the result.
Method #7: Using operator.pow() method
Approach
- Initiate a for loop from i=0 to len(test_list)
- Compute test_list1[i] raised to the power of test_list2[i]
- Append the result to output list
- Display output list
Python3
test_list1 = [ 3 , 5 , 2 , 6 , 4 ]
test_list2 = [ 7 , 3 , 4 , 1 , 5 ]
print ( "The original list 1 is : " + str (test_list1))
print ( "The original list 2 is : " + str (test_list2))
res = []
for i in range ( 0 , len (test_list1)):
import operator
x = operator. pow (test_list1[i],test_list2[i])
res.append( int (x))
print ( "The cross exponentiation list is : " + str (res))
|
Output
The original list 1 is : [3, 5, 2, 6, 4]
The original list 2 is : [7, 3, 4, 1, 5]
The cross exponentiation list is : [2187, 125, 16, 6, 1024]
Time Complexity: O(N), where N is the length of given test_list1 and test_list2
Auxiliary Space: O(N) as it creates a new list of size N to store the result.
Last Updated :
28 Mar, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...