Python | Sum of squares in list
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
# Python code to demonstrate# sum of squares# using reduce() + lambda# initializing listtest_list = [3, 5, 7, 9, 11]# printing original listprint("The original list is : " + str(test_list))# using reduce() + lambda# sum of squaresres = reduce(lambda i, j: i + j * j, [test_list[:1][0]**2]+test_list[1:])# printing resultprint("The sum of squares of list is : " + str(res)) |
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
# Python3 code to demonstrate# sum of squares# using sum() + max()# initializing listtest_list = [3, 5, 7, 9, 11]# printing original listprint("The original list is : " + str(test_list))# using sum() + max()# sum of squaresres = sum(map(lambda i: i * i, test_list))# printing resultprint("The sum of squares of list is : " + str(res)) |
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
# Python3 code to demonstrate# sum of squares# initializing listtest_list = [3, 5, 7, 9, 11]# printing original listprint("The original list is : " + str(test_list))# sum of squaresres = 0for i in test_list: from math import pow res += pow(i, 2)res = int(res)# printing resultprint("The sum of squares of list is : " + str(res)) |
The original list is : [3, 5, 7, 9, 11] The sum of squares of list is : 285
Method : Using simple list comprehension
Here is an approach using a list comprehension:
Python3
# initializing listtest_list = [3, 5, 7, 9, 11]# printing original listprint("The original list is : " + str(test_list))# using list comprehension# sum of squaresres = sum([i**2 for i in test_list])# printing resultprint("The sum of squares of list is : " + str(res))# This code is contributed by Edula Vinay Kumar Reddy |
The original list is : [3, 5, 7, 9, 11] The sum of squares of list is : 285
Method #5: Using numpy
Python3
import numpy as nptest_list = np.array([3, 5, 7, 9, 11])# printing original listprint("The original list is : ", test_list)# using NumPy# sum of squaresres = np.sum(np.square(test_list))# printing resultprint("The sum of squares of list is : ", res)#This code is contributed by jyothi pinjala. |
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)






Please Login to comment...