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

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 "sum the last n elements of list python"

Python Answers by Framework

Browse Popular Code Answers by Language