Open In App

Python program to compute the power by Index element in List

Last Updated : 15 Apr, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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




# Python3 code to demonstrate working of
# Index Power List
# Using ** operator + loop + enumerate()
 
# initializing list
test_list = [6, 9, 1, 8, 4, 7]
              
# printing original list
print("The original list is : " + str(test_list))
 
# ** does task of getting power
res = []
for idx, ele in enumerate(test_list):
  res.append(ele ** idx)
 
# printing result
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




# Python3 code to demonstrate working of
# Index Power List
# Using pow() + list comprehension + enumerate()
from math import pow
 
# initializing list
test_list = [6, 9, 1, 8, 4, 7]
              
# printing original list
print("The original list is : " + str(test_list))
 
# pow() does task of getting power
# list comprehension for 1 liner alternative
res = [int(pow(ele, idx)) for idx, ele in enumerate(test_list)]
 
# printing result
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:

  1. Initiated for loop from i=0 to len(test_list)
  2. Raised each element to the power of i using operator.pow() and appended to output list
  3. Display output list

Python3




# Python3 code to demonstrate working of
# Index Power List
 
# initializing list
test_list = [6, 9, 1, 8, 4, 7]
             
# printing original list
print("The original list is : " + str(test_list))
 
# ** does task of getting power
res = []
import operator
for i in range(0,len(test_list)):
    res.append(operator.pow(test_list[i],i))
     
# printing result
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




# Python3 code to demonstrate working of index
# Power List using numpy.power() function
 
# importing numpy module
import numpy as np
 
# initializing list
test_list = [6, 9, 1, 8, 4, 7]
 
# printing original list
print("The original list is : " + str(test_list))
 
# creating numpy array from list
arr = np.array(test_list)
 
# getting powered elements
res = np.power(arr, range(len(test_list)))
 
# converting numpy array to list
res = res.tolist()
 
# printing result
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:

  1. Initialize an empty list res to store the powered elements.
  2. Loop through the list using the enumerate() function.
  3. Use reduce() and count() from itertools to get the power of each element.
  4. Append the powered element to the res list.
  5. Return the res list as output.

Python3




import itertools
from functools import reduce
 
# initializing list
test_list = [6, 9, 1, 8, 4, 7]
 
# printing original list
print("The original list is : " + str(test_list))
 
# using reduce() and count() from itertools to get the power of each element
res = [reduce(lambda x, _: x*ele, range(i), 1) for i, ele in enumerate(test_list)]
 
# printing result
print("Powered elements : " + str(res))
#This code is contributed by Jyothi Pinjala.


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)



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads