Skip to content
Related Articles
Open in App
Not now

Related Articles

Python | Sum of squares in list

Improve Article
Save Article
  • Last Updated : 08 Feb, 2023
Improve Article
Save Article

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 list
test_list = [3, 5, 7, 9, 11]
 
# printing original list
print("The original list is : " + str(test_list))
 
# using reduce() + lambda
# sum of squares
res = reduce(lambda i, j: i + j * j, [test_list[:1][0]**2]+test_list[1:])
 
# printing result
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




# Python3 code to demonstrate
# sum of squares
# using sum() + max()
 
# initializing list
test_list = [3, 5, 7, 9, 11]
 
# printing original list
print("The original list is : " + str(test_list))
 
# using sum() + max()
# sum of squares
res = sum(map(lambda i: i * i, test_list))
 
# printing result
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




# Python3 code to demonstrate
# sum of squares
 
# initializing list
test_list = [3, 5, 7, 9, 11]
 
# printing original list
print("The original list is : " + str(test_list))
 
 
# sum of squares
res = 0
for i in test_list:
    from math import pow
    res += pow(i, 2)
res = int(res)
# printing result
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

Method : Using simple list comprehension

Here is an approach using a list comprehension:

Python3




# initializing list
test_list = [3, 5, 7, 9, 11]
 
# printing original list
print("The original list is : " + str(test_list))
 
# using list comprehension
# sum of squares
res = sum([i**2 for i in test_list])
 
# printing result
print("The sum of squares of list is : " + str(res))
 
# This code is contributed by Edula Vinay Kumar Reddy

Output

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 np
 
test_list = np.array([3, 5, 7, 9, 11])
 
# printing original list
print("The original list is : ", test_list)
 
# using NumPy
# sum of squares
res = np.sum(np.square(test_list))
 
# printing result
print("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)


My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!