Sunday, November 17, 2024
Google search engine
HomeLanguagesPython | Tail Sliced List Summation

Python | Tail Sliced List Summation

We often come to the situations in which we need to decrease the size of the list by truncating the last elements of the list and perform remaining list summation. This particular problem occurs when we need to optimize memory. This has its application in the day-day programming when sometimes we require to get all the lists of similar size. Let’s discuss certain ways in which this task can be performed.
Method #1 : Using len() + list slicing + sum() 
List slicing can perform this particular task in which we just slice the first len(list) – K elements to be in the list and hence removing the last K elements. The task of performing tail summation is performed using sum().

Python3




# Python code to demonstrate
# Tail Sliced List Summation
# using len() + list slicing + sum()
 
# initializing list
test_list = [1, 4, 6, 3, 5, 8]
 
# printing original list
print ("The original list is : " + str(test_list))
 
# initializing K
K = 3
 
# using len() + list slicing + sum()
# Tail Sliced List Summation
res = sum(test_list[: len(test_list) - K])
 
# printing result
print ("The tail removed list summation : " + str(res))


Output

The original list is : [1, 4, 6, 3, 5, 8]
The tail removed list summation : 11

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

 Method #2 : Using Negative list slicing + sum() 
We can perform this particular task using the negative list slicing in which we start removing the elements from the last index of the list and hence remove all the K elements from the last. We remove None if 0 elements are asked to be removed. The task of performing tail summation is performed using sum().

Python3




# Python code to demonstrate
# Tail Sliced List Summation
# using negative list slicing + sum()
 
# initializing list
test_list = [1, 4, 6, 3, 5, 8]
 
# printing original list
print ("The original list is : " + str(test_list))
 
# initializing K
K = 3
 
# using negative list slicing + sum()
# Tail Sliced List Summation
res = sum(test_list[: -K or None])
 
# printing result
print ("The tail removed list summation : " + str(res))


Output

The original list is : [1, 4, 6, 3, 5, 8]
The tail removed list summation : 11

Time Complexity: O(n) where n is the number of elements in the test_list. The Negative list slicing + sum() is used to perform the task and it takes O(n) time.
Auxiliary Space: O(n) additional space of size n is created where n is the number of elements in the test list.

Method #3 : Using itertools.islice() + sum()
We can also perform this task using the itertools module. This module provides us with a function called islice() which takes an iterable as input and outputs only the required elements from it. Using this function, we can obtain only the first len(list) – K elements from the list and hence obtain the list which doesn’t contain the last K elements. The task of performing tail summation is performed using sum().

Python3




# Python code to demonstrate
# Tail Sliced List Summation
# using itertools.islice() + sum()
 
# importing itertools module
import itertools
 
# initializing list
test_list = [1, 4, 6, 3, 5, 8]
 
# printing original list
print ("The original list is : " + str(test_list))
 
# initializing K
K = 3
 
# using itertools.islice() + sum()
# Tail Sliced List Summation
res = sum(itertools.islice(test_list, 0, len(test_list) - K))
 
# printing result
print ("The tail removed list summation : " + str(res))


Output

The original list is : [1, 4, 6, 3, 5, 8]
The tail removed list summation : 11

Time Complexity: O(N) where N is the length of the list.
Auxiliary Space: O(1)

Explanation: Here, itertools.islice() function is used to slice the list and return only the required elements. The sum function is then used to calculate the sum of all the elements in the sliced list.

Method #4 : Using while loop + pop() method

Python3




# Python code to demonstrate
# Tail Sliced List Summation
 
# initializing list
test_list = [1, 4, 6, 3, 5, 8]
 
# printing original list
print ("The original list is : " + str(test_list))
 
# initializing K
K = 3
i=0
while(i<K):
    test_list.pop()
    i+=1
res=sum(test_list)
# printing result
print ("The tail removed list summation : " + str(res))


Output

The original list is : [1, 4, 6, 3, 5, 8]
The tail removed list summation : 11

Time Complexity: O(N) where N is the length of the list.
Auxiliary Space: O(1)

Dominic Rubhabha-Wardslaus
Dominic Rubhabha-Wardslaushttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Recent Comments