Python | Sum of squares in list
Last Updated :
02 May, 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 squares of list in just one line. Let’s discuss certain ways in which this can be performed.
Method #1 : Using reduce() + lambda 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. Works with only Python 2.
Python
test_list = [ 3 , 5 , 7 , 9 , 11 ]
print ( "The original list is : " + str (test_list))
res = reduce ( lambda i, j: i + j * j, [test_list[: 1 ][ 0 ] * * 2 ] + test_list[ 1 :])
print ( "The sum of squares of list is : " + str (res))
|
Output
The original list is : [3, 5, 7, 9, 11]
The sum of squares of list is : 285
Time Complexity: The reduce function in this code has a time complexity of O(n), where n is the number of elements in the input list “test_list”. The lambda function used inside the reduce function takes constant time to execute, so the overall time complexity is O(n).
Auxiliary Space: The reduce function in this code has an auxiliary space complexity of O(1), as it only requires a single accumulator variable to store the intermediate and final results. The lambda function used inside the reduce function takes constant space, so the overall auxiliary space complexity is O(1).
Method #2 : Using map() + sum() The similar solution can also be obtained using the map function to integrate and sum function to perform the summation of the squared number.
Python3
test_list = [ 3 , 5 , 7 , 9 , 11 ]
print ( "The original list is : " + str (test_list))
res = sum ( map ( lambda i: i * i, test_list))
print ( "The sum of squares of list is : " + str (res))
|
Output
The original list is : [3, 5, 7, 9, 11]
The sum of squares of list is : 285
Time Complexity: O(n)
Auxiliary Space: O(1)
Method #3 : Using for loop and math.pow()
Python3
test_list = [ 3 , 5 , 7 , 9 , 11 ]
print ( "The original list is : " + str (test_list))
res = 0
for i in test_list:
from math import pow
res + = pow (i, 2 )
res = int (res)
print ( "The sum of squares of list is : " + str (res))
|
Output
The original list is : [3, 5, 7, 9, 11]
The sum of squares of list is : 285
Time Complexity: O(n), where n is the number of elements in the input list.
Auxiliary Space: O(1), as we are not using any additional data structure or creating any new list to store intermediate results.
Method #4 : Using simple list comprehension
Here is an approach using a list comprehension:
Python3
test_list = [ 3 , 5 , 7 , 9 , 11 ]
print ( "The original list is : " + str (test_list))
res = sum ([i * * 2 for i in test_list])
print ( "The sum of squares of list is : " + str (res))
|
Output
The original list is : [3, 5, 7, 9, 11]
The sum of squares of list is : 285
Time Complexity: O(n), where n is the length of the input list, as the list comprehension iterates over each element in the input list once.
Auxiliary Space: O(1), as the space used by the algorithm is constant and does not depend on the size of the input.
Method #5: Using numpy
Python3
import numpy as np
test_list = np.array([ 3 , 5 , 7 , 9 , 11 ])
print ( "The original list is : " , test_list)
res = np. sum (np.square(test_list))
print ( "The sum of squares of list is : " , res)
|
Output:
The original list is : [3, 5, 7, 9, 11]
The sum of squares of list is : 285
Time Complexity: O(N)
Auxiliary Space : O(1)
Approach using reduce() and operator.mul():
The reduce() function from the functools module is used to reduce the list to a single value by applying a given function to the elements of the list. The operator.mul() function from the operator module is used to multiply the squared element of the list. In this approach, we use reduce() and operator.mul() to calculate the sum of the squares of the elements in the input list.
Algorithm:
Import reduce() and operator.mul() functions from the respective modules.
Take the input list as input_list.
Use reduce() function with operator.mul() and map() function to calculate the sum of squares of input_list.
Print the result.
Python3
from functools import reduce
import operator
input_list = [ 3 , 5 , 7 , 9 , 11 ]
print ( "The original list is : " , input_list)
res = reduce (operator.add, map ( lambda x: x * x, input_list))
print ( "The sum of squares of list is : " , res)
|
Output
The original list is : [3, 5, 7, 9, 11]
The sum of squares of list is : 285
Time complexity: The time complexity of this approach is O(n) because map() and reduce() function iterate over the input list once.
Auxiliary space: The space complexity of this approach is O(1) because we are not using any additional data structure or creating any new list to store intermediate results.
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...