 GeeksforGeeks App
Open App Browser
Continue

# Python – Get sum of last K list items using slice

Accessing elements in a list has many types and variations. These are an essential part of Python programming and one must have the knowledge to perform the same. This article discusses ways to fetch the last K elements and do its summation. Let’s discuss certain solution to perform this task.

Method #1 : Using list slicing + sum()
This problem can be performed in 1 line rather than using a loop using the list slicing functionality provided by Python and then using sum(). Minus operator specifies slicing to be done from rear end.

## Python3

 `# Python3 code to demonstrate``# Inverse K slice Sum``# using list slicing + sum()` `# initializing list``test_list ``=` `[``4``, ``5``, ``2``, ``6``, ``7``, ``8``, ``10``]` `# printing original list``print``(``"The original list : "` `+` `str``(test_list))` `# initializing K``K ``=` `5` `# Inverse K slice Sum``# using list slicing + sum()``res ``=` `sum``(test_list[``-``K:])` `# print result``print``(``"The last K elements sum of list is : "` `+` `str``(res))`

Output :

```The original list : [4, 5, 2, 6, 7, 8, 10]
The last K elements sum of list is : 33```

Time Complexity: O(n), The above code iterates through the list once, hence the time complexity is linear, i.e. O(n).
Space Complexity: O(n), The algorithm uses an additional list to store the result, thus consuming linear space which is O(n).

Method #2 : Using islice() + reversed() + sum()
The inbuilt functions can also be used to perform this particular task. The islice function can be used to get the sliced list and reversed function is used to get the elements from rear end and then sum() can be employed to perform summation.

## Python3

 `# Python3 code to demonstrate``# Inverse K slice Sum``# using islice() + reversed() + sum()``from` `itertools ``import` `islice` `# initializing list``test_list ``=` `[``4``, ``5``, ``2``, ``6``, ``7``, ``8``, ``10``]` `# printing original list``print``(``"The original list : "` `+` `str``(test_list))` `# initializing K``K ``=` `5` `# using islice() + reversed() + sum()``# Inverse K slice Sum``res ``=` `list``(islice(``reversed``(test_list), ``0``, K))``res.reverse()``res ``=` `sum``(res)` `# print result``print``(``"The last K elements sum of list are : "` `+` `str``(res))`

Output :

```The original list : [4, 5, 2, 6, 7, 8, 10]
The last K elements sum of list is : 33```

Time Complexity: O(n), where n is the length of the input list. This is because we’re using islice() + reversed() + sum() which has a time complexity of O(n) in the worst case.
Auxiliary Space: O(1), as we’re using constant additional space

Method #3: Using a loop to iterate over the last K elements of the list and summing them up.

Use a for loop to iterate over the last K elements of the list and sum them up. Start by initializing the variable res to 0, and then use a range function to generate a sequence of integers from 1 to K, then use this sequence to access the last K elements of the list one by one and add them to res. Finally, print the result.

## Python3

 `# Python3 code to demonstrate``# Inverse K slice Sum``# using loop to iterate over last K elements` `# initializing list``test_list ``=` `[``4``, ``5``, ``2``, ``6``, ``7``, ``8``, ``10``]` `# printing original list``print``(``"The original list : "` `+` `str``(test_list))` `# initializing K``K ``=` `5` `# Inverse K slice Sum``# using loop to iterate over last K elements``res ``=` `0``for` `i ``in` `range``(``1``, K``+``1``):``    ``res ``+``=` `test_list[``-``i]` `# print result``print``(``"The last K elements sum of list is : "` `+` `str``(res))`

Output

```The original list : [4, 5, 2, 6, 7, 8, 10]
The last K elements sum of list is : 33```

Time complexity: O(K), The loop iterates K times, so the time complexity of this method is O(K).
Auxiliary space: O(1), This method has a constant auxiliary space complexity of O(1), because we only use one extra variable (res) to store the sum of the last K elements.

Method #4 : Using operator.getitem(),slice(),sum(),len() methods

Approach

1. Slice the list from K to end of list using len(),operator.getitem(),slice() methods
2. Find the sum of sliced list using sum()
3. Display sum

## Python3

 `# Python3 code to demonstrate``# Inverse K slice Sum` `# initializing list``test_list ``=` `[``4``, ``5``, ``2``, ``6``, ``7``, ``8``, ``10``]` `# printing original list``print``(``"The original list : "` `+` `str``(test_list))` `# initializing K``K ``=` `5` `# Inverse K slice Sum``import` `operator``ln``=``len``(test_list)``x``=``operator.getitem(test_list,``slice``(ln``-``K,ln))``res ``=` `sum``(x)` `# print result``print``(``"The last K elements sum of list is : "` `+` `str``(res))`

Output

```The original list : [4, 5, 2, 6, 7, 8, 10]
The last K elements sum of list is : 33```

Time complexity: O(K), The loop iterates K times, so the time complexity of this method is O(K).
Auxiliary space: O(1), This method has a constant auxiliary space complexity of O(1), because we only use one extra variable (res) to store the sum of the last K elements.

My Personal Notes arrow_drop_up