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
# Python3 code to demonstrate # Cross list exponentiation # using zip() + list comprehension # initializing lists test_list1 = [ 3 , 5 , 2 , 6 , 4 ] test_list2 = [ 7 , 3 , 4 , 1 , 5 ] # printing original lists print ("The original list 1 is : " + str (test_list1)) print ("The original list 2 is : " + str (test_list2)) # Cross list exponentiation # using zip() + list comprehension res = [i * * j for i, j in zip (test_list1, test_list2)] # printing result print ("The cross exponentiation list is : " + str (res)) |
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
# Python3 code to demonstrate # Cross list exponentiation # using map() from operator import pow # initializing lists test_list1 = [ 3 , 5 , 2 , 6 , 4 ] test_list2 = [ 7 , 3 , 4 , 1 , 5 ] # printing original lists print ("The original list 1 is : " + str (test_list1)) print ("The original list 2 is : " + str (test_list2)) # Cross list exponentiation # using map() res = list ( map ( pow , test_list1, test_list2)) # printing result print ("The cross exponentiation list is : " + str (res)) |
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
# Python3 code to demonstrate # Cross list exponentiation # initializing lists test_list1 = [ 3 , 5 , 2 , 6 , 4 ] test_list2 = [ 7 , 3 , 4 , 1 , 5 ] # printing original lists print ( "The original list 1 is : " + str (test_list1)) print ( "The original list 2 is : " + str (test_list2)) # Cross list exponentiation res = [] for i in range ( 0 , len (test_list1)): import math x = math. pow (test_list1[i],test_list2[i]) res.append( int (x)) # printing result print ( "The cross exponentiation list is : " + str (res)) |
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
# Python3 code to demonstrate # Cross list exponentiation # using numpy.power() # Importing numpy import numpy as np # initializing lists test_list1 = [ 3 , 5 , 2 , 6 , 4 ] test_list2 = [ 7 , 3 , 4 , 1 , 5 ] # printing original lists print ( "The original list 1 is : " + str (test_list1)) print ( "The original list 2 is : " + str (test_list2)) # Cross list exponentiation # using numpy.power() res = np.power(test_list1, test_list2) # printing result print ( "The cross exponentiation list is : " + str (res)) #This code is contributed by Edula Vinay Kumar Reddy |
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
# initializing the lists test_list1 = [ 3 , 5 , 2 , 6 , 4 ] test_list2 = [ 7 , 3 , 4 , 1 , 5 ] # create an empty list to store the results res = [] # iterate over the indices of the lists for i in range ( len (test_list1)): # calculate the cross exponentiation of the i-th element of both lists and append it to the result list res.append(test_list1[i] * * test_list2[i]) # print the result print ( "The cross exponentiation list is : " , res) |
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
# Python3 code to demonstrate # Cross list exponentiation # using a list comprehension and a lambda function # initializing lists test_list1 = [ 3 , 5 , 2 , 6 , 4 ] test_list2 = [ 7 , 3 , 4 , 1 , 5 ] # printing original lists print ( "The original list 1 is : " + str (test_list1)) print ( "The original list 2 is : " + str (test_list2)) # Cross list exponentiation # using a list comprehension and a lambda function res = [( lambda x, y: x * * y)(x, y) for x, y in zip (test_list1, test_list2)] # printing result print ( "The cross exponentiation list is : " + str (res)) |
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
# Python3 code to demonstrate # Cross list exponentiation # initializing lists test_list1 = [ 3 , 5 , 2 , 6 , 4 ] test_list2 = [ 7 , 3 , 4 , 1 , 5 ] # printing original lists print ( "The original list 1 is : " + str (test_list1)) print ( "The original list 2 is : " + str (test_list2)) # Cross list exponentiation res = [] for i in range ( 0 , len (test_list1)): import operator x = operator. pow (test_list1[i],test_list2[i]) res.append( int (x)) # printing result print ( "The cross exponentiation list is : " + str (res)) |
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.