Python program to compute the power by Index element in List
Last Updated :
15 Apr, 2023
Given a list, the task is to write a Python program to compute the power of each element by its index value.
Input : test_list = [6, 9, 1, 8, 4, 7]
Output : [1, 9, 1, 512, 256, 16807]
Explanation : 8 * 8 * 8 = 512, as 8 is at 3rd index.
Input : test_list = [6, 9, 1, 8]
Output : [1, 9, 1, 512]
Explanation : 9**1 = 9, as 9 is at 1st index.
Method 1 : Using ** operator + loop + enumerate()
In this, the task of getting power is done using the ** operator and loop is used to iterate through elements. The enumerate() is used to get index along with values.
Python3
test_list = [ 6 , 9 , 1 , 8 , 4 , 7 ]
print ( "The original list is : " + str (test_list))
res = []
for idx, ele in enumerate (test_list):
res.append(ele * * idx)
print ( "Powered elements : " + str (res))
|
Output:
The original list is : [6, 9, 1, 8, 4, 7]
Powered elements : [1, 9, 1, 512, 256, 16807]
Time Complexity: O(n*n)
Auxiliary Space: O(n)
Method 2 : Using pow() + list comprehension + enumerate()
In this, we perform the task of getting power using pow(), enumerate() is used to get index with values.
Python3
from math import pow
test_list = [ 6 , 9 , 1 , 8 , 4 , 7 ]
print ( "The original list is : " + str (test_list))
res = [ int ( pow (ele, idx)) for idx, ele in enumerate (test_list)]
print ( "Powered elements : " + str (res))
|
Output:
The original list is : [6, 9, 1, 8, 4, 7]
Powered elements : [1, 9, 1, 512, 256, 16807]
Time Complexity: O(n) where n is the number of elements in the list “test_list”. The pow() + list comprehension + enumerate() is used to perform the task and it takes O(n) time.
Auxiliary Space: O(n), new list of size O(n) is created where n is the number of elements in the list
Method 3 : Using operator.pow() and for loop
Approach:
- Initiated for loop from i=0 to len(test_list)
- Raised each element to the power of i using operator.pow() and appended to output list
- Display output list
Python3
test_list = [ 6 , 9 , 1 , 8 , 4 , 7 ]
print ( "The original list is : " + str (test_list))
res = []
import operator
for i in range ( 0 , len (test_list)):
res.append(operator. pow (test_list[i],i))
print ( "Powered elements : " + str (res))
|
Output
The original list is : [6, 9, 1, 8, 4, 7]
Powered elements : [1, 9, 1, 512, 256, 16807]
Time Complexity : O(N)
Auxiliary Space : O(N)
Method 4: Using map() function and lambda expression
This code creates a range() object with the same length as test_list, then applies the lambda function to each element of the range. The lambda function takes an index i and returns test_list[i] ** i. The map() function applies this function to each element of the range and returns an iterator, which is converted to a list using the list() function.
Python3
test_list = [ 6 , 9 , 1 , 8 , 4 , 7 ]
res = list ( map ( lambda i: pow (test_list[i], i), range ( len (test_list))))
print ( "Powered elements : " + str (res))
|
Output
Powered elements : [1, 9, 1, 512, 256, 16807]
Time complexity: O(n)
Auxiliary space: O(n) since it creates a new list to store the powered elements.
Method 5: Using numpy.power() function
Steps:
- Import the numpy module using the import statement.
- Initialize the list of numbers.
- Create a numpy array from the list using numpy.array() function.
- Use numpy.power() function with the array as the first argument and the indices as the second argument.
- Convert the resulting numpy array to a list using the tolist() method.
- Print the list of powered elements.
Python3
import numpy as np
test_list = [ 6 , 9 , 1 , 8 , 4 , 7 ]
print ( "The original list is : " + str (test_list))
arr = np.array(test_list)
res = np.power(arr, range ( len (test_list)))
res = res.tolist()
print ( "Powered elements : " + str (res))
|
Output:
The original list is : [6, 9, 1, 8, 4, 7]
Powered elements : [1, 9, 1, 512, 256, 16807]
Time Complexity: O(n), where n is the length of the input list.
Auxiliary Space: O(n), for storing the powered elements in the list.
Method 6: Using reduce():
Algorithm:
- Initialize an empty list res to store the powered elements.
- Loop through the list using the enumerate() function.
- Use reduce() and count() from itertools to get the power of each element.
- Append the powered element to the res list.
- Return the res list as output.
Python3
import itertools
from functools import reduce
test_list = [ 6 , 9 , 1 , 8 , 4 , 7 ]
print ( "The original list is : " + str (test_list))
res = [ reduce ( lambda x, _: x * ele, range (i), 1 ) for i, ele in enumerate (test_list)]
print ( "Powered elements : " + str (res))
|
Output
The original list is : [6, 9, 1, 8, 4, 7]
Powered elements : [1, 9, 1, 512, 256, 16807]
Time Complexity: O(n^2)
The outer loop runs for n times, where n is the length of the input list.
The inner loop runs for i times, where i is the index of the current element in the list.
The reduce() function also runs for i times.
Therefore, the time complexity of this algorithm is O(n^2).
Space Complexity: O(n)
An extra list res is created to store the powered elements.
Therefore, the space complexity of this algorithm is O(n)
Share your thoughts in the comments
Please Login to comment...