Wednesday, July 3, 2024
HomeLanguagesPythonPython | Above K elements summation

Python | Above K elements summation

Many times we might have problem in which we need to find summation rather than the actual numbers and more often, the result is conditioned.. Let’s discuss certain ways in which this problem can be successfully solved. 
Method #1 : Using loop This problem can easily be solved using loop with a brute force approach in which we can just check for the sum as we iterate and append it in a new list as we proceed forward. 

Python3




# Python3 code to demonstrate
# Above K elements summation
# using loop
 
# initializing list
test_list = [12, 10, 18, 15, 8, 18]
 
# printing original list
print("The original list : " + str(test_list))
 
# using loop
# Above K elements summation
res = 0
for idx in range(0, len(test_list)) :
    if test_list[idx] > 10:
        res += test_list[idx]
 
# print result
print("The summation of elements greater than 10 : " + str(res))


Output

The original list : [12, 10, 18, 15, 8, 18]
The summation of elements greater than 10 : 63

Time Complexity: O(n), where n is the length of the input list. This is because we’re using the loop which has a time complexity of O(n) in the worst case.
Auxiliary Space: O(n), as we’re using additional space res other than the input list itself with the same size of input list. 

  Method #2 : Using list comprehension + sum() The combination of these two function can also perform this particular task efficiently and in one line. The sum function is used to perform summation of elements. 

Python3




# Python3 code to demonstrate
# Above K elements summation
# using list comprehension + sum()
 
# initializing list
test_list = [12, 10, 18, 15, 8, 18]
 
# printing original list
print("The original list : " + str(test_list))
 
# using list comprehension + sum()
# index of matching element
res = sum(ele for ele in test_list if ele > 10)
 
# print result
print("The summation of elements greater than 10 : " + str(res))


Output

The original list : [12, 10, 18, 15, 8, 18]
The summation of elements greater than 10 : 63

Time Complexity: O(n), where n is the number of elements in the list “test_list”.
Auxiliary Space: O(n), where n is the number of elements in the list “test_list”.

Method #3 : Using filter() + sum()

Python3




#This method is similar to the second method, but uses the filter() function to filter the elements in the list.
def greater_than_10(x):
  return x > 10
 
test_list = [12, 10, 18, 15, 8, 18]
 
#printing original list
print("The original list : " + str(test_list))
 
#using filter() + sum()
#index of matching element
res = sum(filter(greater_than_10, test_list))
 
#print result
print("The summation of elements greater than 10 : " + str(res))
#This code is contributed by Edula Vinay Kumar Reddy


Output

The original list : [12, 10, 18, 15, 8, 18]
The summation of elements greater than 10 : 63

In Method #3, filter() function is used to filter the elements in the list that satisfy a certain condition and only those elements are passed to the sum() function which then returns the summation of those elements.

Time Complexity: O(n) where n is the size of the list and 
Auxiliary Space: O(1)

Thapelo Manthata
I’m a desktop support specialist transitioning into a SharePoint developer role by day and Software Engineering student by night. My superpowers include customer service, coding, the Microsoft office 365 suite including SharePoint and power platform.
RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments