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
Python Program to repeat elements at custom indices
Given a List, the following program repeats those elements which are at a custom index, these custom indices are provided to it as a separate list. Input : test_list = [4, 6, 7, 3, 1, 9, 2, 19], idx_list = [3, 1, 6] Output : [4, 6, 6, 7, 3, 3, 1, 9, 2, 2, 19] Explanation : All required index element
6 min read
Python program to fetch the indices of true values in a Boolean list
Given a list of only boolean values, write a Python program to fetch all the indices with True values from given list. Let's see certain ways to do this task. Method #1: Using itertools [Pythonic way] itertools.compress() function checks for all the elements in list and returns the list of indices w
5 min read
Replace index elements with elements in Other List-Python
The task of replacing index elements with elements from another list involves mapping the indices from one list to the corresponding elements in a second list. For each index in the first list, the element at that index is retrieved from the second list and stored in a new result list. For example,
4 min read
Python program to a Sort Matrix by index-value equality count
Given a Matrix, the task is to write a Python program that can sort its rows or columns on a measure of the number of values equal to its index number. For each row or column, count occurrences of equality of index number with value. After computation of this count for each row or column, sort the m
6 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 | List Element Count with Order
Sometimes, while working with lists or numbers we can have a problem in which we need to attach with each element of list, a number, which is the position of that element's occurrence in that list. This type of problem can come across many domains. Let's discuss a way in which this problem can be so
4 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 - Count elements in tuple list
Sometimes, while working with data in form of records, we can have a problem in which we need to find the count of all the records received. This is a very common application that can occur in Data Science domain. Letâs discuss certain ways in which this task can be performed. Method #1: Using len()
5 min read