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
# 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:
- 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
# 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))
OutputThe 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))
OutputPowered 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:
- 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
# 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.
OutputThe 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)
Similar Reads
Python Program to find the cube of each list element Given a list, the task is to write a python program to cube all the list elements. Input: [1, 2, 3, 4] Output: [1, 8, 27, 64] Explanation: Cubing all the list elementsInput: [2, 4, 6] Output: [8, 64, 216] Method 1: Using loop This is the brute force way. In this, we just multiply the same element tw
4 min read
How to Get the Number of Elements in a Python List In Python, lists are one of the most commonly used data structures to store an ordered collection of items.In this article, we'll learn how to find the number of elements in a given list with different methods.ExampleInput: [1, 2, 3.5, geeks, for, geeks, -11]Output: 7Let's explore various ways to do
3 min read
Python - Operation to each element in list Given a list, there are often when performing a specific operation on each element is necessary. While using loops is a straightforward approach, Python provides several concise and efficient methods to achieve this. In this article, we will explore different operations for each element in the list.
2 min read
Python - Negative index of Element in List We are given a list we need to find the negative index of that element. For example, we are having a list li = [10, 20, 30, 40, 50] and the given element is 30 we need to fin the negative index of it so that given output should be -3.Using index()index() method in Python searches for the first occur
3 min read
Python program to find power of a number The task of finding the power of a number in Python involves calculating the result of raising a base number to an exponent. For example, if we have a base 2 and an exponent 3, the result is 2^3=8 .Using ** operatorThis is the simplest and most Pythonic way to calculate the power of a number. The **
2 min read