Python | i^k Summation in list
Last Updated : 22 Apr, 2023
Python being the language of magicians can be used to perform many tedious and repetitive tasks in a easy and concise manner and having the knowledge to utilize this tool to the fullest is always useful. One such small application can be finding sum of i^k of list in just one line. Let’s discuss certain ways in which this can be performed.
Method #1 : Using reduce() + lambda + pow() The power of lambda functions to perform lengthy tasks in just one line, allows it combined with reduce which is used to accumulate the subproblem, to perform this task as well. The pow() is used to perform task of computing power. Works with only Python 2.
Python3
test_list = [ 3 , 5 , 7 , 9 , 11 ] print ("The original list is : " + str (test_list)) K = 4 res = reduce ( lambda i, j: i + pow (j, K), [ pow (test_list[: 1 ][ 0 ], K)] + test_list[ 1 :]) print ("The sum of i ^ k of list is : " + str (res)) |
Output : The original list is : [3, 5, 7, 9, 11] The sum of i^k of list is : 24309
Time complexity: O(n*n), where n is the length of the test_list. The reduce() + lambda + pow() takes O(n*n) time
Auxiliary Space: O(n), extra space of size n is required
Method #2 : Using map() + sum() + pow() The similar solution can also be obtained using the map function to integrate and sum function to perform the summation of the i^k number.
Python3
test_list = [ 3 , 5 , 7 , 9 , 11 ] print ("The original list is : " + str (test_list)) K = 4 res = sum ( map ( lambda i : pow (i, K), test_list)) print ("The sum of i ^ k of list is : " + str (res)) |
Output : The original list is : [3, 5, 7, 9, 11] The sum of i^k of list is : 24309
Time Complexity: O(n*n), where n is the length of the input list. This is because we’re using map() + sum() + pow() which has a time complexity of O(n*n) in the worst case.
Auxiliary Space: O(1), as we’re using constant additional space
Method #4 : Using Numpy
Note: Install numpy module using command “pip install numpy”
Python3
import numpy as np test_list = [ 3 , 5 , 7 , 9 , 11 ] print ( "The original list is : " + str (test_list)) K = 4 res = np. sum (np.power(test_list, K)) print ( "The sum of i ^ k of list is : " + str (res)) |
Output:
The original list is : [3, 5, 7, 9, 11]
The sum of i ^ k of list is : 24309
This method uses the numpy module to perform the computation. The np.power() function is used to compute the power of each element in the input list, and the np.sum() function is used to add up the results.
Time Complexity: O(n)
Space Complexity: O(n)
Method #4 : Using a for loop:
Python3
test_list = [ 3 , 5 , 7 , 9 , 11 ] print ( "The original list is : " + str (test_list)) K = 4 res = 0 for i in test_list: res + = pow (i, K) print ( "The sum of i ^ k of list is : " + str (res)) |
Output The original list is : [3, 5, 7, 9, 11] The sum of i ^ k of list is : 24309
Time Complexity: O(n)
Space Complexity: O(1)
Method#5: Using Recursive method.
Algorithm:
- Define a function sum_of_powers(test_list, K) that takes a list test_list and an integer K as inputs.
- If test_list is empty, return 0 as the base case.
- Otherwise, compute the sum of the first element of test_list raised to the power of K, and the sum of the rest of the
- elements of test_list computed recursively using the same function sum_of_powers with the tail of test_list.
- Return the sum computed in step 3 as the final result.
Python3
def sum_of_powers(test_list, K): if len (test_list) = = 0 : return 0 else : return pow (test_list[ 0 ], K) + sum_of_powers(test_list[ 1 :], K) test_list = [ 3 , 5 , 7 , 9 , 11 ] K = 4 print ( "The original list is : " + str (test_list)) res = sum_of_powers(test_list, K) print ( "The sum of i ^ k of list is : " + str (res)) |
Output The original list is : [3, 5, 7, 9, 11] The sum of i ^ k of list is : 24309
Time complexity:
The time complexity of this recursive implementation of the code is O(n), where n is the length of the input list test_list.
Space complexity:
The space complexity of the recursive implementation is O(n) as well, where n is the length of the input list test_list.
Similar Reads
Python - Key Lists Summations
Sometimes, while working with Python Dictionaries, we can have problem in which we need to perform the replace of key with values with sum of all keys in values. This can have application in many domains that include data computations, such as Machine Learning. Let's discuss certain ways in which th
9 min read
Python | Equal Keys List Summation
Sometimes, while working with dictionaries, we can have a problem in which we have many dictionaries and we are required to sum like keys. This problem seems common, but complex is if the values of keys are list and we need to add elements to list of like keys. Letâs discuss way in which this proble
4 min read
Python - Index Value Summation List
To access the elements of lists, there are various methods. But sometimes we may require to access the element along with the index on which it is found and compute its summation, and for that, we may need to employ different strategies. This article discusses some of those strategies. Method 1: Nai
4 min read
Python | Summation of tuples in list
Sometimes, while working with records, we can have a problem in which we need to find the cumulative sum of all the values that are present in tuples. This can have applications in cases in which we deal with a lot of record data. Let's discuss certain ways in which this problem can be solved. Metho
7 min read
Python - Summation Grouping in Dictionary List
Sometimes, while working with Python Dictionaries, we can have a problem in which we need to perform the grouping of dictionaries according to specific key, and perform summation of certain keys while grouping similar key's value. This is s peculiar problem but can have applications in domains such
5 min read
Element indices Summation - Python
Our task is to calculate the sum of elements at specific indices in a list. This means selecting elements at specific positions and computing their sum. Given a list and a set of indices, the goal is to compute the sum of elements present at those indices. For example, given the list [10, 20, 30, 40
3 min read
Python | Alternate element summation in list
The problem of getting summation of a list is quite generic and we might some day face the issue of getting the summation of alternate elements and get the list of 2 elements containing summation of alternate elements. Let's discuss certain ways in which this can be performed. Method #1 : Using list
5 min read
Python | Summation of dictionary list values
Sometimes, while working with Python dictionaries, we can have its values as lists. In this can, we can have a problem in that we just require the count of elements in those lists as a whole. This can be a problem in Data Science in which we need to get total records in observations. Let's discuss c
6 min read
Python | Accumulative index summation in tuple list
Sometimes, while working with data, we can have a problem in which we need to find accumulative summation of each index in tuples. This problem can have applications in web development and competitive programming domain. Let's discuss certain way in which this problem can be solved. Method 1: Using
8 min read
Python | Pair summation of list elements
Sometimes, while working with Python list, one can have a problem in which one needs to find perform the summation of list in pair form. This is useful as a subproblem solution of bigger problem in web development and day-day programming. Let's discuss certain ways in which this problem can be solve
4 min read