Open In App

Python | Size and element exponentiation of list

Improve
Improve
Like Article
Like
Save
Share
Report

Sometimes, while working with Python lists, we can have a problem in which we need to extend a list in a very customized way. We may have to repeat the contents of the list and while doing that, each time new list must be exponentiation of the original list. This incremental expansion has applications in many domains. Let’s discuss a way in which this task can be performed.

Method 1: Using list comprehension This task can be performed in a brute manner, but having a shorter implementation using list comprehension always is better. In this, we perform task in 2 steps, first we make a helper list to form an exponentiation factor list and then cumulate the result using the original list. 

Python3




# Python3 code to demonstrate working of
# Size and element exponentiation of list
# Using list comprehension
 
# initializing list
test_list = [4, 5, 6]
 
# printing original list
print("The original list is : " + str(test_list))
 
# Extension factor
N = 4
 
# Exponentiation factor
M = 3
 
# Size and element exponentiation of list
# Using list comprehension
temp = [1 * M**i for i in range(N)]
res = list([ele ** tele for tele in temp for ele in test_list])
 
# printing result
print("List after extension and exponentiation : " + str(res))


Output

The original list is : [4, 5, 6]
List after extension and exponentiation : [4, 5, 6, 64, 125, 216, 262144, 1953125, 10077696, 18014398509481984, 7450580596923828125, 1023490369077469249536]

Time complexity: O(N * M)
Auxiliary Space: O(N * M)

Method 2: Using while loop and extend() method

Python3




# Python3 code to demonstrate working of
# Size and element exponentiation of list
 
# initializing list
test_list = [4, 5, 6]
 
# printing original list
print("The original list is : " + str(test_list))
 
# Extension factor
N = 4
 
# Exponentiation factor
M = 3
j = 1
res = []
res.extend(test_list)
# Size and element exponentiation of list
while(j < N):
    a = []
    for i in test_list:
        a.append(i**3)
    res.extend(a)
    test_list = a
    j += 1
 
 
# printing result
print("List after extension and exponentiation : " + str(res))


Output

The original list is : [4, 5, 6]
List after extension and exponentiation : [4, 5, 6, 64, 125, 216, 262144, 1953125, 10077696, 18014398509481984, 7450580596923828125, 1023490369077469249536]

Time complexity: O(n^2)
Auxiliary space: O(n^2)

Method 3: Using list comprehension and reduce() function

This method uses the reduce() function to iterate N-1 times and extend the list with the cubed values of the last len(test_list) elements of the list. It uses a lambda function to define the logic of the extension. 

Python3




from functools import reduce
 
# initializing list
test_list = [4, 5, 6]
 
# printing original list
print("The original list is : " + str(test_list))
 
# Extension factor
N = 4
 
# Exponentiation factor
M = 3
 
# Size and element exponentiation of list
res = reduce(lambda x, _: x + [e**M for e in x[-len(test_list):]], range(N-1), test_list)
 
# printing result
print("List after extension and exponentiation : " + str(res))


Output

The original list is : [4, 5, 6]
List after extension and exponentiation : [4, 5, 6, 64, 125, 216, 262144, 1953125, 10077696, 18014398509481984, 7450580596923828125, 1023490369077469249536]

Time complexity: O(NM)
Auxiliary space: O(NM).

METHOD 4 :  Using recursion.

Step by step approach:

Define a function that takes a list, extension factor (N), and exponentiation factor (M) as parameters.
Check if N is greater than 1. If yes, then recurse by calling the function again with the updated list obtained by raising each element to the power M, N-1 as the new extension factor, and M as the exponentiation factor.
If N is equal to 1, return the original list.
Finally, return the extended list obtained from the recursive calls.

Python3




# Python3 code to demonstrate working of
# Size and element exponentiation of list using recursion
 
# initializing list
test_list = [4, 5, 6]
 
# printing original list
print("The original list is : " + str(test_list))
 
# Extension factor
N = 4
 
# Exponentiation factor
M = 3
 
# Define a recursive function
def extend_and_exponentiate(lst, n, m):
    if n > 1:
        new_lst = [i**m for i in lst]
        return lst + extend_and_exponentiate(new_lst, n-1, m)
    else:
        return lst
 
# Size and element exponentiation of list using recursion
res = extend_and_exponentiate(test_list, N, M)
 
# printing result
print("List after extension and exponentiation : " + str(res))


Output

The original list is : [4, 5, 6]
List after extension and exponentiation : [4, 5, 6, 64, 125, 216, 262144, 1953125, 10077696, 18014398509481984, 7450580596923828125, 1023490369077469249536]

Time Complexity: O(NM) where N is the extension factor and M is the exponentiation factor, as we are raising each element to the power M N times.
Auxiliary Space: O(NM) as the size of the extended list will be N*M.



Last Updated : 05 May, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads