Answers for "python sum of last n elements in list"

2

python sum of last n items in list

test_list = [4, 5, 2, 6, 7, 8, 10] 
K = 5
  
# Inverse K slice Sum using list slicing + sum()
res = sum(test_list[-K:]) 

print("The last K elements sum of list is : " + str(res)) 

# Result:
# The last K elements sum of list is : 33
Posted by: Guest on July-12-2021

Code answers related to "python sum of last n elements in list"

Python Answers by Framework

Browse Popular Code Answers by Language