python sum of last n items in list

Solutions on MaxInterview for python sum of last n items in list by the best coders in the world

showing results for - "python sum of last n items in list"
Clara
17 Aug 2020
1test_list = [4, 5, 2, 6, 7, 8, 102K = 5
3  
4# Inverse K slice Sum using list slicing + sum()
5res = sum(test_list[-K:]) 
6
7print("The last K elements sum of list is : " + str(res)) 
8
9# Result:
10# The last K elements sum of list is : 33